Ich verbringe viel Zeit damit, Informationen mit scrapy ohne Erfolg zu verschrotten. Mein Ziel ist es, durch Kategorie und für jeden Artikel Schrott Titel, Preis und Titel href Link zu surfen.Scrapy kann keine Artikel verschrotten, xpath funktioniert nicht
Das Problem scheint von der Funktion parse_items zu kommen. Ich habe überprüfen XPath mit firepath und ich bin in der Lage, die Elemente auszuwählen, als wollte, vielleicht fange ich einfach nicht, wie XPath von scrapy verarbeitet werden ...
Hier ist mein Code
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from scrapy.selector import Selector
from ..items import electronic_Item
class robot_makerSpider(CrawlSpider):
name = "robot_makerSpider"
allowed_domains = ["robot-maker.com"]
start_urls = [
"http://www.robot-maker.com/shop/",
]
rules = (
Rule(LinkExtractor(
allow=(
"http://www.robot-maker.com/shop/12-kits-robots",
"http://www.robot-maker.com/shop/36-kits-debutants-arduino",
"http://www.robot-maker.com/shop/13-cartes-programmables",
"http://www.robot-maker.com/shop/14-shields",
"http://www.robot-maker.com/shop/15-capteurs",
"http://www.robot-maker.com/shop/16-moteurs-et-actionneurs",
"http://www.robot-maker.com/shop/17-drivers-d-actionneurs",
"http://www.robot-maker.com/shop/18-composants",
"http://www.robot-maker.com/shop/20-alimentation",
"http://www.robot-maker.com/shop/21-impression-3d",
"http://www.robot-maker.com/shop/27-outillage",
),
),
callback='parse_items',
),
)
def parse_items(self, response):
hxs = Selector(response)
products = hxs.xpath("//div[@id='center_column']/ul/li")
items = []
for product in products:
item = electronic_Item()
item['title'] = product.xpath(
"li[1]/div/div/div[2]/h2/a/text()").extract()
item['price'] = product.xpath(
"div/div/div[3]/div/div[1]/span[1]/text()").extract()
item['url'] = product.xpath(
"li[1]/div/div/div[2]/h2/a/@href").extract()
#check that all field exist
if item['title'] and item['price'] and item['url']:
items.append(item)
return items
danke für deine Hilfe
danke mann! Ich werde von hier vorsichtig sein. Können Sie mir erklären, wie sich die Suche nach dem xpath direkt aus der Antwort ergibt, anstatt die Selector (response) -Methode zu verwenden? –
@ArtFilPortraitArtistetissu es ist im Wesentlichen die gleiche Sache. Response-Objekt erstellt Selector mit sich selbst, so dass Sie eine bequeme Verknüpfung zu "response.selector" haben und Selector nicht jedes Mal selbst erstellen müssen. Und 'response.xpath' ist eine Verknüpfung für' response.selector.xpath'. Die [Quelle für Antwort] (https://github.com/scrapy/scrapy/blob/master/scrapy/http/response/text.py#L112) ist ziemlich einfach und Sie können es selbst einen Höhepunkt geben :) – Granitosaurus