gpt4 book ai didi

Python Scrapy 打印 start_url 或 start_url 中的变量

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

尝试产生“数字”或者可能获取start_url,然后解析start_url以获取数字:

class EbaypriceSpider(Spider):
name = "ebayprice"
allowed_domains = ["www.ebay.com"]
start_urls = []
with open('Numbers.csv', 'rb') as omcan_numbers:
number_list = csv.reader(omcan_numbers)
for number in number_list:
start_urls.append('http://www.ebay.com/sch/Omcan' + str(number))


def parse(self, response):
# DO stuff then call parse_page2


def parse_page2(self, response):
print number
# I want to get get start url or number

最佳答案

使用 start_requests 方法代替 start_urls:

class EbaypriceSpider(Spider):
name = "ebayprice"
allowed_domains = ["www.ebay.com"]

def start_requests(self):
with open('Numbers.csv','rb') as omcan_numbers:
number_list = csv.reader(omcan_numbers)
for number in number_list:
url = 'http://www.ebay.com/sch/Omcan'+ str(number)
yield Request(url, meta={'start_url':url}, callback=self.parse)

def parse(self, response):
# DO stuff then call parse_page2
...
# keep passing the `meta` argument from previous request
yield Request(some_other_url, meta=response.meta, callback=self.parse_page2)

def parse_page2(self, response):
# i want to get get start url or number
start_url = response.meta['start_url']

关于Python Scrapy 打印 start_url 或 start_url 中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42307370/

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