试图让我的网络爬虫抓取从网页中提取的链接。我正在使用 Scrapy。我可以用我的爬虫成功地提取数据,但不能让它爬。我相信问题出在我的规则部分。Scrapy 新手。感谢您提前提供帮助。
我正在抓取这个网站:
http://ballotpedia.org/wiki/index.php/Category:2012_challenger
我试图关注的链接在源代码中如下所示:
/wiki/index.php/A._Ghani
或者
/wiki/index.php/A._Keith_Carreiro
这是我的蜘蛛的代码:
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.contrib.spiders import CrawlSpider,Rule
from ballot1.items import Ballot1Item
class Ballot1Spider(CrawlSpider):
name = "stewie"
allowed_domains = ["ballotpedia.org"]
start_urls = [
"http://ballotpedia.org/wiki/index.php/Category:2012_challenger"
]
rules = (
Rule (SgmlLinkExtractor(allow=r'w+'), follow=True),
Rule(SgmlLinkExtractor(allow=r'\w{4}/\w+/\w+'), callback='parse')
)
def parse(self, response):
hxs = HtmlXPathSelector(response)
sites = hxs.select('*')
items = []
for site in sites:
item = Ballot1Item()
item['candidate'] = site.select('/html/head/title/text()').extract()
item['position'] = site.select('//table[@class="infobox"]/tr/td/b/text()').extract()
item['controversies'] = site.select('//h3/span[@id="Controversies"]/text()').extract()
item['endorsements'] = site.select('//h3/span[@id="Endorsements"]/text()').extract()
item['currentposition'] = site.select('//table[@class="infobox"]/tr/td[@style="text-align:center; background-color:red;color:white; font-size:100%; font-weight:bold;"]/text()').extract()
items.append(item)
return items