我最喜欢的功能之一 requests是我可以将查询参数指定为字典:
r = requests.get('https://example.com/posts', params={
'count': '12',
'start': '56',
'order': 'new'
})
我目前正在 Scrapy 中构建一个网络抓取工具。有没有办法指定查询参数的字典,而不是将所有内容硬编码到 url 中?
我目前被迫做这样的事情:
scrapy.Request('https://example.com/posts?count=%d&start=%d&order=%s' %
(12, 56, 'new',))
有没有一种方法可以做我想要的事情,类似于上面的请求的方式?
最佳答案
from urllib import urlencode
params = {
'count': '12',
'start': '56',
'order': 'new'
}
paramstr = urlencode(params) # 'count=12&start=56&order=new'
scrapy.Request('https://example.com/posts?' + paramstr)
关于python - 请求变量作为字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31617915/