use*_*717 16 python web-crawler scrapy
我的问题是如何做与前一个问题相同的事情,但在Scrapy 0.14中.
基本上,我有GUI,它采用域,关键字,标签名称等参数,我想创建一个通用的蜘蛛来抓取那些域中的那些关键字.我通过覆盖蜘蛛管理器类或动态创建蜘蛛来阅读使用旧版scrapy的冲突内容.首选哪种方法,如何实施和调用正确的解决方案?提前致谢.
这是我想要通用的代码.它还使用BeautifulSoup.我把它配对了所以希望没有删除任何关键的理解它.
class MySpider(CrawlSpider):
name = 'MySpider'
allowed_domains = ['somedomain.com', 'sub.somedomain.com']
start_urls = ['http://www.somedomain.com']
rules = (
Rule(SgmlLinkExtractor(allow=('/pages/', ), deny=('', ))),
Rule(SgmlLinkExtractor(allow=('/2012/03/')), callback='parse_item'),
)
def parse_item(self, response):
contentTags = []
soup = BeautifulSoup(response.body)
contentTags = soup.findAll('p', itemprop="myProp")
for contentTag in contentTags:
matchedResult = re.search('Keyword1|Keyword2', contentTag.text)
if matchedResult:
print('URL Found: ' + response.url)
pass
Run Code Online (Sandbox Code Playgroud)
我不确定哪种方式是首选,但我会告诉你我过去做了什么。我绝对不确定这是最好的(或正确的)方法,我有兴趣了解其他人的想法。
我通常只是重写父类 ( ) 并传入参数,然后通过我自己的 init 函数CrawlSpider初始化父类,或者从数据库中提取该数据,在该数据库中我保存了要附加到之前的链接列表。super(MySpider, self).__init__()start_urls
| 归档时间: |
|
| 查看次数: |
5863 次 |
| 最近记录: |