gpt4 book ai didi

python - 尝试抓取,找回[]

转载 作者:行者123 更新时间:2023-12-01 08:54:54 27 4
gpt4 key购买 nike

我正在使用 Scrapy 1.5.1Python 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
}

但是,当尝试抓取该网站时,我得到了 []

我通过 shell 检查了我的 xpath,一切似乎都正常。

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

最佳答案

某些配置文件 URL 根本不存在,因此 XPath 表达式计算结果为空。

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

但是,您还需要指定一个起始网址,例如:start_urls = ['https://bitcointalk.org'] 或仅添加 start_requests 函数。

这里引用了 Scrapy 文档中有关 start_urls 1 的内容。 ...

Instead of implementing a start_requests() method that generates scrapy. Request objects from URLs, you can just define a start_urls class attribute with a list of URLs.

关于python - 尝试抓取,找回[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52842572/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com