私は成功なしで情報をスクラップでスクラップしようと多くの時間を費やします。 私の目標は、カテゴリーをサーフし、各アイテムのスクラップタイトル、価格、タイトルのhrefリンクです。スクラップでアイテムをスクラップできない、xpathが機能しない
問題はparse_items関数から来ているようです。ここで
は私のコードです...私はfirepathとXPathをチェックしていると私は望んでいたとして、項目を選択することができるよ、ので、多分私はちょうどscrapyによって処理される方法のxpathキャッチしない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
あなたの助けてくれてありがとう
感謝の男!私はここから慎重になるでしょう。 Selector(レスポンス)メソッドを使用する代わりに、xpathをレスポンスから直接探すことの影響について教えてください。 –
@ArtFilPortraitArtistetisuそれは本質的に同じことです。レスポンスオブジェクトはそれ自身を使って 'Selector'を作成するので、' response.selector'へのショートカットを便利にして、毎回セレクタを作成する必要はありません。そして 'response.xpath'は' response.selector.xpath'のショートカットです。 [Response for](https://github.com/scrapy/scrapy/blob/master/scrapy/http/response/text.py#L112)はとてもシンプルで、あなたはそれを自分自身で最大限に引き出すことができます:) – Granitosaurus