我正在尝试让爬虫工作,但 SgmlLinkExtractor 似乎存在问题。
这是签名:
SgmlLinkExtractor(allow=(), deny=(), allow_domains=(), deny_domains=(), restrict_xpaths(), tags=('a', 'area'), attrs=('href'), canonicalize=True,唯一=真,过程值=无)
我正在使用 allow() 选项,这是我的代码:
start_urls = ['http://bigbangtrans.wordpress.com']
rules = [Rule(SgmlLinkExtractor(allow=[r'series-\d{1}-episode-\d{2}.']), callback='parse_item')]
示例 url 看起来像http://bigbangtrans.wordpress.com/series-1-episode-11-the-pancake-batter-anomaly/
scrapy crawl tbbt
包含的输出
[tbbt] 调试:已爬网(200)http://bigbangtrans.wordpress.com/series-3-episode-17-the-precious-fragmentation/>(参考:http ://bigbangtrans.wordpress.com )
但是,没有调用 parse_item 回调,我不知道为什么。
这是整个蜘蛛代码:
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
class TbbtSpider(CrawlSpider):
#print '\n TbbtSpider \n'
name = 'tbbt'
start_urls = ['http://bigbangtrans.wordpress.com'] # urls from which the spider will start crawling
rules = [Rule(SgmlLinkExtractor(allow=[r'series-\d{1}-episode-\d{2}.']), callback='parse_item')]
def parse_item(self, response):
print '\n parse_blogpost \n'
hxs = HtmlXPathSelector(response)
item = TbbtItem()
# Extract title
item['title'] = hxs.select('//div[@id="post-5"]/div/p/span/text()').extract() # XPath selector for title
return item