代码之家  ›  专栏  ›  技术社区  ›  Carol.Kar

试着去刮,回来

  •  1
  • Carol.Kar  · 技术社区  · 7 年前

    Scrapy 1.5.1 Python 2.7.6 . 我试着从下面的代码中提取用户名 page

    我实现了以下代码:

    # -*- coding: utf-8 -*-
    import scrapy
    from scrapy.http import Request
    
    
    class BtctalkspiderSpider(scrapy.Spider):
        name = 'btctalkSpider'
        allowed_domains = ['bitcointalk.org']
        max_uid = 10
    
        def parse(self, response):
            urls = response.xpath("//a/@href").extract()
            for i in range(self.max_uid):
                # scrapy shell "https://bitcointalk.org/index.php?action=profile;u=1"
                yield Request('https://bitcointalk.org/index.php?action=profile;u=%d' % i, callback=self.parse_application)
    
        def parse_application(self, response):
            userName = response.xpath('//td[normalize-space(.)="Name:"]/following-sibling::td/text()').extract()
    
    
            yield {
                'userName': userName
            }
    

    [] 回来。

    我检查了我的房间 xpath 通过外壳,一切似乎都正常。

    有什么建议我做错了什么吗?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Ahmed Saad    7 年前

    一些概要文件url根本不存在,因此XPath表达式的计算结果为nothing。

    例如: https://bitcointalk.org/index.php?action=profile;u=2

    但是,您还需要为ex: start_urls = ['https://bitcointalk.org'] 或者直接加上 start_requests

    这是一个引用从剪贴簿文件关于 start_urls 1 ...

    而不是实现生成 粘糊糊的。从URL请求对象,您只需定义一个起始URL 具有URL列表的类属性。