0
我目前正在在Python开发web爬虫,将采取多个CPU内核的优势。这是我第一次尝试多处理,我在实现和概念方面遇到了一些问题。到目前为止,我只是为了开发而产生一个进程,它将抓取一个网站并获取我想要的内容,这不是问题。当我想从一个页面被儿童解析的网址发送回队列将所有的子进程能够访问这个队列,以获得更多的工作Python的多网络爬虫问题
我的问题开始。
有了这个,我不知道如何去开始这个程序有多个进程,让他们不断获得/做的工作,而一旦做了,让他们加入()。由于网址的数量显然是一个大而未知的数字,我无法在终止之前设置一些事情。
我欢迎任何和所有的批评和批评,因为我是一个萌芽的Python开发。我也知道这是一个相当复杂的问题,非常感谢社区的帮助!
请不要建议我使用一些现有的网络爬虫,这对我来说是一个学习的过程比什么都重要,因此使用一些现有的软件抓取,我是不会有帮助。
下面是到目前为止我的代码给你什么,我有一个想法,我明白了什么。
import multiprocessing as mp
import re
import subprocess
import time
from bs4 import BeautifulSoup as bs4
import requests
class CrawlParseProcess(mp.Process):
def __init__(self, url):
mp.Process.__init__(self)
# This implies that a new process per URL which is not smart.
# Instead should the link queue be passed or something?
self.url = url
def run(self):
proc_name = self.name
response = requests.get(self.url)
links = self.link_parse(response.text, self.url)
def link_parse(self, html, url):
soup = bs4(html)
for link in soup.find_all('a'):
if link.has_attr('href'):
link = link.get('href')
if str(link).startswith('http') and 'example.com' in link:
print link
pattern = re.compile('http://www.example.com/.+\d/*')
if pattern.search(url):
return_obj = self.parse(html)
return_obj["date"] = time.time()
def parse(self, html):
soup = bs4(html)
return_obj = {
"url" : self.url,
"title" : soup.find('h1', {'itemprop' : 'name'}).text,
"date-added" : soup.find('div', {'class' : 'addedwhen clrfix'}).text,
"p" : soup.find('p', {'class' : 'time1'}).text,
"c" : soup.find('p', {'class' : 'time2'}).text,
"t" : soup.find('h3', {'class' : 'duration'}).text,
"items" : soup.find_all('li', {'itemprop' : 'items'}),
}
return return_obj
if __name__ == '__main__':
p = CrawlParseProcess("www.example.com")
p.start()
p.join()