【发布时间】:2015-03-20 23:41:21
【问题描述】:
我正在尝试使用 Rule 类转到我的爬虫中的下一页。这是我的代码
from scrapy.contrib.spiders import CrawlSpider,Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from crawler.items import GDReview
class GdSpider(CrawlSpider):
name = "gd"
allowed_domains = ["glassdoor.com"]
start_urls = [
"http://www.glassdoor.com/Reviews/Johnson-and-Johnson-Reviews-E364_P1.htm"
]
rules = (
# Extract next links and parse them with the spider's method parse_item
Rule(SgmlLinkExtractor(restrict_xpaths=('//li[@class="next"]/a/@href',)), follow= True)
)
def parse(self, response):
company_name = response.xpath('//*[@id="EIHdrModule"]/div[3]/div[2]/p/text()').extract()
'''loop over every review in this page'''
for sel in response.xpath('//*[@id="EmployerReviews"]/ol/li'):
review = Item()
review['company_name'] = company_name
review['id'] = str(sel.xpath('@id').extract()[0]).split('_')[1] #sel.xpath('@id/text()').extract()
review['body'] = sel.xpath('div/div[3]/div/div[2]/p/text()').extract()
review['date'] = sel.xpath('div/div[1]/div/time/text()').extract()
review['summary'] = sel.xpath('div/div[2]/div/div[2]/h2/tt/a/span/text()').extract()
yield review
我的问题是关于规则部分。在此规则中,提取的链接不包含域名。例如,它将返回类似 "/Reviews/Johnson-and-Johnson-Reviews-E364_P1.htm"
如何确保我的爬虫将域附加到返回的链接?
谢谢
【问题讨论】:
标签: python web-crawler scrapy scrapy-spider