【发布时间】:2018-09-26 00:49:00
【问题描述】:
我的蜘蛛没有抓取第 2 页,但 XPath 正在返回正确的下一页链接,这是指向下一页的绝对链接。
这是我的代码
from scrapy import Spider
from scrapy.http import Request, FormRequest
class MintSpiderSpider(Spider):
name = 'Mint_spider'
allowed_domains = ['example.com']
start_urls = ['http://www.example.com/']
def parse(self, response):
urls = response.xpath('//div[@class = "post-inner post-hover"]/h2/a/@href').extract()
for url in urls:
yield Request(url, callback=self.parse_lyrics)
next_page_url = response.xpath('//li[@class="next right"]/a/@href').extract_first()
if next_page_url:
yield scrapy.Request(next_page_url, callback=self.parse)
def parse_foo(self, response):
info = response.xpath('//*[@class="songinfo"]/p/text()').extract()
name = response.xpath('//*[@id="lyric"]/h2/text()').extract()
yield{
'name' : name,
'info': info
}
【问题讨论】:
-
请更正缩进。
-
其实缩进是对的,我不小心把它分成了两部分,现在你可以检查一下。
标签: python web-scraping scrapy