我正在尝试使用scrapy从多个网页中提取电话标题(以及最终的其他数据)。我正在尝试使用已定义的函数来做到这一点。"parse“函数应该提取所有页面链接,如果我让它将结果输出到CSV,它确实可以正确地执行此操作。但是,当我尝试设置第二个"parse_pages“时,代码似乎甚至不会尝试处理,并且我无法获得仅包含每个页面标题的CSV输出
注意:我认识到下面的函数缩进是错误的,
import scrapy
from scrapy.http import Request
url = 'https://www.gsmarena.com/'
class PhonelinksSpider(scrapy.Spider):
name = 'phonelinks'
allowed_domains = ['www.gsmarena.com/results.php3?']
start_urls = ['https://www.gsmarena.com/results.php3?']
def parse(self, response):
links = response.xpath('//div[@class="makers"]/ul/li/a/@href').extract()
for link in links:
location = url+link
yield response.follow(url = location,callback = self.parse_pages)
def parse_pages(self, response):
phones = response.xpath('//h1[contains(@class,"specs-phone-name-title")]/text()').extract_first().strip()
for title in phones:
phone_list = {'phone':title}
yield phone_list发布于 2019-11-12 05:30:55
这里
phones = response.xpath('//h1[contains(@class,"specs-phone-name-title")]/text()').extract_first().strip()extract_first()返回一个string或None,这就是为什么您可以在下一行迭代它。
def parse_pages(self, response):
title = response.xpath('//h1[contains(@class,"specs-phone-name-title")]/text()').extract_first().strip()
yield {'phone':title}https://stackoverflow.com/questions/58808251
复制相似问题