0
方法でparse()
スパイダーは4つのURLをクロールし、一部のデータを掻き集めるメソッドparse_dir_contents()
に送信しますが、4番目のURLのみが掻き取られています。なぜ他の3つのURLを掻いていないのですか?私はparse_dir_contents
機能でfor
ループの必要がないと思ってページを調べてこの蜘蛛の蜘蛛は何ですか?最後のURLだけを掻きます
import scrapy
from v_one.items import VOneItem
import json
class linkedin(scrapy.Spider):
name = "linkedin"
allowed_domains = ["linkedin.com"]
start_urls = [
"https://in.linkedin.com/directory/people-s-1-2-4/",
]
def parse(self, response):
for href in response.xpath('//*[@id="seo-dir"]/div/div/div/ul/li/a/@href'):
url = response.urljoin(href.extract())
print "________________"+url
yield scrapy.Request(url, callback=self.parse_dir_contents)
def parse_dir_contents(self, response):
for sel in response.xpath('//*[@id="profile"]'):
url = response.url
print "____________"+url
item = VOneItem()
item['name'] = sel.xpath('//*[@id="name"]/text()').extract()
item['headline'] = sel.xpath('//*[@id="topcard"]/div/div/div/p/span/text()').extract()
item['current'] = sel.xpath('//*[@id="topcard"]/div/div/div/table/tbody/tr/td/ol/li/span/text()').extract()
item['education'] = sel.xpath('//*[@id="topcard"]/div/div/div/table/tbody/tr/td/ol/li/a/text()').extract()
item['link'] = url
yield item
あなたのコードはすべてのページ/リンクを既に訪問しているので、どのURLが各URLを削っているのかわかりません。また、あなたのxpathsは非常に脆いです、より正確にデータを取得するクラス名がたくさんあります。また、通常はブラウザーによって追加されますので、実際にはそこには存在しないかもしれません –