{"id":1561,"date":"2023-03-25T11:26:10","date_gmt":"2023-03-25T03:26:10","guid":{"rendered":""},"modified":"2023-03-25T11:26:10","modified_gmt":"2023-03-25T03:26:10","slug":"Python\u722c\u866b\u591a\u7ebf\u7a0b","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1561.html","title":{"rendered":"Python\u722c\u866b\u591a\u7ebf\u7a0b"},"content":{"rendered":"
\n
Python \u591a\u7ebf\u7a0b\u8be6\u7ec6\u6559\u7a0b<\/span>\n <\/div>\n 1\u3001\u8bbe\u7f6eGIL\u3002<\/span> 1\u3001\u6784\u9020\u4efb\u52a1\u961f\u5217pageQueue \uff0c\u5b58\u653e\u6240\u6709\u8981\u722c\u53d6\u7684\u9875\u9762url\u3002<\/span> <\/body>Python\u591a\u7ebf\u7a0b<\/h2>\n
\n 2\u3001\u5207\u6362\u5230\u4e00\u4e2a\u7ebf\u7a0b\u53bb\u6267\u884c\u3002<\/span>
\n 3\u3001\u8fd0\u884c\u3002<\/span>
\n 4\u3001\u628a\u7ebf\u7a0b\u8bbe\u7f6e\u4e3a\u7761\u7720\u72b6\u6001\u3002<\/span>
\n 5\u3001\u89e3\u9501GIL\u3002<\/span>
\n 6\u3001\u518d\u6b21\u91cd\u590d\u4ee5\u4e0a\u6b65\u9aa4\u3002<\/span> <\/p>\n
\n Python\u591a\u7ebf\u7a0b\u6559\u7a0b\n <\/div>\n\u7ebf\u7a0b\u5b89\u5168\u7684\u961f\u5217 Queue<\/h2>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-09-11<\/span>
#python 2
<\/span> import <\/span>Queue
# python 3
<\/span> from <\/span>queue import <\/span>Queue
<\/span><\/code><\/pre>\n<\/p><\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-09-11<\/span>
# \u5b9e\u4f8b\u5316\u4e00\u4e2a\u961f\u5217\uff0c\u53ef\u4ee5\u5728\u6307\u5b9a\u961f\u5217\u5927\u5c0f
<\/span> q = Queue.Queue<\/span>()
q_50 = Queue.Queue<\/span>(50) # \u6307\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a50\u7684\u961f\u5217
<\/span> # \u5165\u961f\u4e00\u4e2a\u6570\u636edata
<\/span> q.put<\/span>(data)
# \u51fa\u961f\u5e76\u8d4b\u503c\u7ed9item
<\/span> item = q.get<\/span>()
# \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\uff0c\u662f\u5426\u6ee1
<\/span> if <\/span>q.empty<\/span>()\uff1a
print('\u961f\u5217\u4e3a\u7a7a'<\/span>)
if <\/span>q.full<\/span>():
print('\u961f\u5217\u6ee1'<\/span>)
<\/span><\/code><\/pre>\n<\/p><\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-21<\/span>
from <\/span>pyquery import <\/span>PyQuery as <\/span>pq
doc = pq(filename='example.html'<\/span>)
print doc.html<\/span>()
print type(doc)
li = doc('li'<\/span>)
print type(li)
print li.text<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u4e09\u3001\u57fa\u4e8e\u591a\u7ebf\u7a0b\u722c\u866b\u722c\u53d6\u7cd7\u4e8b\u767e\u79d1\u7684\u6bb5\u5b50<\/h2>\n
\n 2\u3001\u7528\u591a\u7ebf\u7a0b\u722c\u866b\u4ece\u7cd7\u4e8b\u767e\u79d1\u4e0a\u6293\u53d6\u7cd7\u4e8b\uff0c\u7136\u540e\u5c06\u6293\u53d6\u7684\u9875\u9762\u5185\u5bb9\u5b58\u653e\u5230data_queue\u4e2d<\/span>
\n 3\u3001\u7528\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u5bf9data_queue\u4e2d\u7684\u9875\u9762\u5185\u5bb9\u8fdb\u884c\u89e3\u6790\uff0c\u5206\u522b\u63d0\u53d6 \u7cd7\u4e8b\u7684\u56fe\u7247url\uff0c\u7cd7\u4e8b\u7684\u9898\u76ee\u548c\u7cd7\u4e8b\u5185\u5bb9\uff0c\u7136\u540e\u5b58\u653e\u5230\u7684json\u6587\u4ef6\u4e2d\uff08\u4e00\u4e2a\u65f6\u95f4\u70b9\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u5199\u6587\u4ef6IO,\u6ce8\u610f\u5230Python\u7684\u591a\u7ebf\u7a0b\u673a\u5236\u4f7f\u7528\u4e86GIL\u9501\uff09 <\/span> <\/p>\n # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-09-11<\/span>
import <\/span>requests
from <\/span>lxml import <\/span>etree
from <\/span>queue import <\/span>Queue
import <\/span>threading
import <\/span>json
''<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>'
Queue.qsize<\/span>(\u961f\u5217\u540d) #\u8fd4\u56de\u961f\u5217\u7684\u5927\u5c0f
<\/span> Queue.empty<\/span>(\u961f\u5217\u540d) # \u961f\u5217\u4e3a\u7a7a\u8fd4\u56detrue\uff0c\u5426\u5219\u4e3afalse
<\/span> Queue.full<\/span>(\u961f\u5217\u540d) # \u961f\u5217\u6ee1\u8fd4\u56detrue
<\/span> Queue.get<\/span>(\u961f\u5217\u540d,\u503c) # \u51fa\u961f
<\/span> Queue.put<\/span>(\u961f\u5217\u540d,\u503c) # \u5165\u961f
<\/span> FIFO \u5148\u8fdb\u5148\u51fa
'''
class <\/span>Crawl_thread(threading.Thread):
'''
\u6293\u53d6\u7ebf\u7a0b\u7c7b\uff0c\u6ce8\u610f\u9700\u8981\u7ee7\u627f\u7ebf\u7a0b\u7c7bThread
'''
def <\/span>__init__(self,thread_id,queue):
threading.Thread.__init__<\/span>(self) # \u9700\u8981\u5bf9\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\u8fdb\u884c\u521d\u59cb\u5316
<\/span> self.thread_id = thread_id
self.queue = queue # \u4efb\u52a1\u961f\u5217
<\/span> def <\/span>run(self):
'''
\u7ebf\u7a0b\u5728\u8c03\u7528\u8fc7\u7a0b\u4e2d\u5c31\u4f1a\u8c03\u7528\u5bf9\u5e94\u7684run\u65b9\u6cd5
:return:
'''
print('\u542f\u52a8\u7ebf\u7a0b\uff1a'<\/span><\/span>,self.thread_id)
self.crawl_spider<\/span>()
print('\u9000\u51fa\u4e86\u8be5\u7ebf\u7a0b\uff1a'<\/span><\/span>,self.thread_id)
def <\/span>crawl_spider(self):
while True<\/span>:
if <\/span>self.queue.empty<\/span>(): #\u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u8df3\u51fa
<\/span> break
else:<\/span>
page = self.queue.get<\/span>()
print('\u5f53\u524d\u5de5\u4f5c\u7684\u7ebf\u7a0b\u4e3a\uff1a'<\/span>,self.thread_id,\" \u6b63\u5728\u91c7\u96c6\uff1a\"<\/span>,page)
url = 'https:\/\/www.qiushibaike.com\/Shr\/page\/{}\/'<\/span>.format<\/span>(str(page))
headers = {
'User-Agent'<\/span>:'Mozilla\/5.0 <\/span>(Windows NT 6.1) AppleWebKit\/537.36 <\/span>(KHTML, like Gecko) Chrome\/67.0.3371.0 Safari\/537.36'<\/span>
}
try:
content = requests.get<\/span>(url,headers=headers)
data_queue.put<\/span>(content.text) # \u5c06\u91c7\u96c6\u7684\u7ed3\u679c\u653e\u5165data_queue\u4e2d
<\/span> except <\/span>Exception as <\/span>e:
print('\u91c7\u96c6\u7ebf\u7a0b\u9519\u8bef'<\/span>,e)
class <\/span>Parser_thread(threading.Thread):
'''
\u89e3\u6790\u7f51\u9875\u7684\u7c7b\uff0c\u5c31\u662f\u5bf9\u91c7\u96c6\u7ed3\u679c\u8fdb\u884c\u89e3\u6790\uff0c\u4e5f\u662f\u591a\u7ebf\u7a0b\u65b9\u5f0f\u8fdb\u884c\u89e3\u6790
'''
def <\/span>__init__(self,thread_id,queue,file):
threading.Thread.__init__<\/span>(self)
self.thread_id = thread_id
self.queue = queue
self.file = file
def <\/span>run(self):
print('\u542f\u52a8\u7ebf\u7a0b\uff1a', self.thread_id)
while not <\/span>flag:
try:
item = self.queue.get<\/span>(False) # get\u53c2\u6570\u4e3afalse\u65f6\u961f\u5217\u4e3a\u7a7a\uff0c\u4f1a\u629b\u51fa\u5f02\u5e38
<\/span> if <\/span>not item:
pass
self.parse_data<\/span>(item)
self.queue.task_done<\/span>() # \u6bcf\u5f53\u53d1\u51fa\u4e00\u6b21get\u64cd\u4f5c\uff0c\u5c31\u4f1a\u63d0\u793a\u662f\u5426\u5835\u585e
<\/span> except <\/span>Exception as <\/span>e:
pass
print('\u9000\u51fa\u4e86\u8be5\u7ebf\u7a0b\uff1a', self.thread_id)
def <\/span>parse_data(self,item):
'''
\u89e3\u6790\u7f51\u9875\u5185\u5bb9\u7684\u51fd\u6570
:param item:
:return:
'''
try:
html = etree.HTML<\/span>(item)
result = html.xpath<\/span>('\/\/div[contains(@id,\"<\/span>qiushi_tag\")]'<\/span>) # \u5339\u914d\u6240\u6709\u6bb5\u5b50\u5185\u5bb9
<\/span> for <\/span>site in <\/span>result:
try:
img_url = site.xpath<\/span>('.\/\/img\/@src'<\/span>)[0] # \u7cd7\u4e8b\u56fe\u7247
<\/span> title = site.xpath<\/span>('.\/\/h2'<\/span>)[0].text # \u7cd7\u4e8b\u9898\u76ee
<\/span> content = site.xpath<\/span>('.\/\/div[@class=\"<\/span>content\"]\/span'<\/span>)[0].text.strip<\/span>() # \u7cd7\u4e8b\u5185\u5bb9
<\/span> response={
'img_url'<\/span>:img_url,
'title'<\/span>:title,
'content'<\/span>:content
} #\u6784\u9020json
<\/span> json.dump<\/span>(response,fp=self.file,ensure_ascii=False) # \u5b58\u653ejson\u6587\u4ef6
<\/span> except <\/span>Exception as <\/span>e:
print('parse 2: '<\/span>, e)
except <\/span>Exception as <\/span>e:
print('parse 1: '<\/span>,e)
data_queue = Queue() # \u5b58\u653e\u89e3\u6790\u6570\u636e\u7684queue
<\/span> flag = False<\/span>
def <\/span>main():
output = open('qiushi.json'<\/span>,'a'<\/span>,encoding='utf-8'<\/span>) # \u5c06\u7ed3\u679c\u4fdd\u5b58\u5230\u4e00\u4e2ajson\u6587\u4ef6\u4e2d
<\/span> pageQueue = Queue(50) # \u4efb\u52a1\u961f\u5217\uff0c\u5b58\u653e\u7f51\u9875\u7684\u961f\u5217
<\/span> for <\/span>page in <\/span>range(1,11):
pageQueue.put<\/span>(page) # \u6784\u9020\u4efb\u52a1\u961f\u5217
<\/span> # \u521d\u59cb\u5316\u91c7\u96c6\u7ebf\u7a0b
<\/span> crawl_threads = []
crawl_name_list = ['crawl_1'<\/span>,'crawl_2'<\/span>,'crawl_3'<\/span>] # \u603b\u5171\u6784\u90203\u4e2a\u722c\u866b\u7ebf\u7a0b
<\/span> for <\/span>thread_id in <\/span>crawl_name_list:
thread = Crawl_thread(thread_id,pageQueue) # \u542f\u52a8\u722c\u866b\u7ebf\u7a0b
<\/span> thread.start<\/span>() # \u542f\u52a8\u7ebf\u7a0b
<\/span><\/span> crawl_threads.append<\/span>(thread)
# \u521d\u59cb\u5316\u89e3\u6790\u7ebf\u7a0b
<\/span> parse_thread = []
parser_name_list = ['parse_1'<\/span>,'parse_2'<\/span>,'parse_3'<\/span>]
for <\/span>thread_id in <\/span>parser_name_list: #
<\/span> thread = Parser_thread(thread_id,data_queue,output)
thread.start<\/span>() # \u542f\u52a8\u7ebf\u7a0b
parse_thread.append<\/span>(thread)
# \u7b49\u5f85\u961f\u5217\u60c5\u51b5\uff0c\u5148\u8fdb\u884c\u7f51\u9875\u7684\u6293\u53d6
<\/span> while not <\/span>pageQueue.empty<\/span>(): # \u5224\u65ad\u662f\u5426\u4e3a\u7a7a
<\/span> pass <\/span># \u4e0d\u4e3a\u7a7a\uff0c\u5219\u7ee7\u7eed\u963b\u585e
<\/span> # \u7b49\u5f85\u6240\u6709\u7ebf\u7a0b\u7ed3\u675f
<\/span> for <\/span>t in <\/span>crawl_threads:
t.join<\/span>()
# \u7b49\u5f85\u961f\u5217\u60c5\u51b5\uff0c\u5bf9\u91c7\u96c6\u7684\u9875\u9762\u961f\u5217\u4e2d\u7684\u9875\u9762\u8fdb\u884c\u89e3\u6790\uff0c\u7b49\u5f85\u6240\u6709\u9875\u9762\u89e3\u6790\u5b8c\u6210
<\/span> while not <\/span>data_queue.empty<\/span>():
pass
# \u901a\u77e5\u7ebf\u7a0b\u9000\u51fa
<\/span> global<\/span> flag
flag = True<\/span>
for <\/span>t in <\/span>parse_thread:
t.join<\/span>() # \u7b49\u5f85\u6240\u6709\u7ebf\u7a0b\u6267\u884c\u5230\u6b64\u5904\u518d\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c
<\/span> print('\u9000\u51fa\u4e3b\u7ebf\u7a0b'<\/span>)
output.close<\/span>()
if <\/span>__name__ == '__main__'<\/span>:
main()
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python\u722c\u866b\u591a\u7ebf\u7a0bzh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[145],"tags":[],"class_list":["post-1561","post","type-post","status-publish","format-standard","hentry","category-pythonpcjc"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1561"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1561"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1561\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}