【发布时间】:2017-02-27 13:46:50
【问题描述】:
大家好!
我目前正在使用 Python 2.7 开发 Scrapy Webcrawler,虽然我知道 C 和 Web 语言,但我对 Python 和 Scrapy 库有点迷茫。
我想做的是抓取返回 JSON 数据的单个 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命令指定参数,但这会涉及到人为干预,我不希望这样。
我也试过了:
class QuotesSpider(scrapy.Spider):
name = "histo"
tab1=[1000,10]
def start_requests(self, tab1):
for i in 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)
但这似乎也没有办法......
我有点迷茫,所以欢迎任何帮助! :)
非常感谢,祝您有美好的一天!
【问题讨论】:
标签: python python-2.7 scrapy web-crawler