【发布时间】:2016-06-25 09:56:30
【问题描述】:
在 parse() 方法中,蜘蛛抓取 4 个 url,然后发送到方法 parse_dir_contents() 以抓取一些数据,但只抓取了第 4 个 url 我不明白为什么它没有抓取其他 3 个 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
【问题讨论】:
-
您的代码已经访问了每个页面/链接,所以我不知道您如何认为它没有抓取每个网址。此外,您的 xpath 非常脆弱,有很多类名可以更准确地获取数据。 tbody 通常是由浏览器添加的,因此实际上可能并不存在
标签: python web-scraping scrapy web-crawler