我有一个scrapy spider,它接收所需关键字的输入,然后生成一个搜索结果url。然后它抓取该URL,在“item”中获取每个汽车结果的期望值。我正在尝试在我的已生成项目中添加每个完整尺寸的汽车图片链接的网址,该链接伴随着每辆车的结果。在
当我输入关键字“honda”时,正在爬网的特定url如下: Honda search results example
我一直很难找到正确的方法来编写xpath,然后在代码的最后一部分将获得的图像url的列表包含到spider的“item”中。 现在,当项目被保存到一个.csv文件中时lkq.py公司蜘蛛正在用命令“scrapy crawl lkq-o”运行项目.csv-t csv“的列项目.csv图片的文件是全零,而不是图片的url
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import scrapy
from scrapy.shell import inspect_response
from scrapy.utils.response import open_in_browser
keyword = raw_input('Keyword: ')
url = 'http://www.lkqpickyourpart.com/DesktopModules/pyp_vehicleInventory/getVehicleInventory.aspx?store=224&page=0&filter=%s&sp=&cl=&carbuyYardCode=1224&pageSize=1000&language=en-US' % (keyword,)
class Cars(scrapy.Item):
Make = scrapy.Field()
Model = scrapy.Field()
Year = scrapy.Field()
Entered_Yard = scrapy.Field()
Section = scrapy.Field()
Color = scrapy.Field()
Picture = scrapy.Field()
class LkqSpider(scrapy.Spider):
name = "lkq"
allowed_domains = ["lkqpickyourpart.com"]
start_urls = (
url,
)
def parse(self, response):
picture = response.xpath(
'//href=/text()').extract()
section_color = response.xpath(
'//div[@class="pypvi_notes"]/p/text()').extract()
info = response.xpath('//td["pypvi_make"]/text()').extract()
for element in range(0, len(info), 4):
item = Cars()
item["Make"] = info[element]
item["Model"] = info[element + 1]
item["Year"] = info[element + 2]
item["Entered_Yard"] = info[element + 3]
item["Section"] = section_color.pop(
0).replace("Section:", "").strip()
item["Color"] = section_color.pop(0).replace("Color:", "").strip()
item["Picture"] = picture.pop(0).strip()
yield item
我真的不明白您为什么要使用类似于
'//href=/text()'
的xpath,我建议您先阅读一些xpath教程,here是一个非常好的教程。在如果你想得到所有的图片网址,我想这是你想要的
现在,}现在包含了所有的图像链接,只需在那里选择您需要的链接。在
picture.pop(0).strip()
只会得到最后一个url,strip
它,记住,.extract()
返回一个列表,所以{相关问题 更多 >
编程相关推荐