URL from tab中的无用参数

2024-03-29 02:40:03 发布

您现在位置:Python中文网/ 问答频道 /正文

大家好!

我目前正在使用python2.7开发一个Scrapy Webcrawler,虽然我知道C和Web语言,但我对Python和Scrapy库有点迷茫。在

我想做的是抓取一个返回JSON数据的URL,并根据预定义的参数表更改URL中的参数。在

URL如下所示:

http://www.helloworld.com/data?From=xxx&To=yyy&number=42.

这里,我想替换存储在不同文件中的一组数据中的所有xxx,yyy,42,并使用每个参数循环爬虫程序。在

我知道我可以:

    def __init__(self, fromdat='xxx', todat='yyy'):
    self.start_urls = ["http://helloworld.com/data?From=%s&To=%s/" % (fromdat, todat)]

然后在命令行中使用-a命令指定参数,但这将涉及到人工干预,这是我不希望看到的。在

我也试过:

^{pr2}$

但这似乎也起不了什么作用。。。在

我有点迷路了,欢迎任何帮助!:)

提前谢谢你,祝你今天愉快!在


Tags: to数据fromselfcomhttpurldata
1条回答
网友
1楼 · 发布于 2024-03-29 02:40:03

^{}不接受任何其他参数,请执行以下操作:

class QuotesSpider(scrapy.Spider):
    name = "histo"
    tab1 = [1000,10]

    def start_requests(self):
        for i in self.tab1:
            urls = 'http://www.helloworld.com/data?number=%d' % i
            yield scrapy.Request(url=url, callback=self.parse)

    def parse(self, response):
        page = response.url.split("/")[-2]
        filename = 'histo-%s.html' % page
        with open(filename, 'wb') as f:
            f.write(response.body)
        self.log('Saved file %s' % filename)

注意parse()是如何缩进的。在

相关问题 更多 >