【问题标题】:whats wrong with this scrapy spider? scrapes only last url这只scrapy蜘蛛怎么了?只抓取最后一个 url
【发布时间】: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


【解决方案1】:

通过检查页面,我认为parse_dir_contents 函数中不需要for 循环。使函数像这样:

def parse_dir_contents(self, response):
        item = VOneItem()
        item['name'] = response.xpath('//*[@id="name"]/text()').extract()
        item['headline'] = response.xpath('//*[@id="topcard"]/div/div/div/p/span/text()').extract()
        item['current'] = response.xpath('//*[@id="topcard"]/div/div/div/table/tbody/tr/td/ol/li/span/text()').extract()
        item['education'] = response.xpath('//*[@id="topcard"]/div/div/div/table/tbody/tr/td/ol/li/a/text()').extract()
        item['link'] = response.url
        return item

并检查这是否解决了您的问题。

【讨论】:

  • 这种方法是否具有误导性?请不要默默地投票。说明原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多