V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Colorful
V2EX  ›  Python

请教个爬虫传参的问题

  •  
  •   Colorful · 2020-05-16 17:11:33 +08:00 · 1937 次点击
    这是一个创建于 1439 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我的代码是这样写的 idsdata = {'id':100,'skiids':[1,2,3,7]} headersjson={ 'Content-Type':'application/json', 'Accept':'application/json', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36' }

    requests.get(url='http://XXXXX',data=json.dumps(idsdata),headers=headersjson,)

    传参之后在浏览器里面应该是这样的

    id: 100 skiids[]: 1 skiids[]: 2 skiids[]: 3 skiids[]: 7

    但是这行一直报错 应该是传参的格式不对,requests.get(url='http://XXXXX',data=json.dumps(idsdata),headers=headersjson,)

    之前没写过这种,网上没找到太多相关的,看得不太明白

    求教了

    8 条回复    2020-05-16 18:26:00 +08:00
    ClericPy
        1
    ClericPy  
       2020-05-16 17:17:13 +08:00
    直接盲猜啊... 没 Python 版本, 没网址, 没报错信息... 你发个 curl 也行...
    随便回答下吧:
    如果是 Python3, data 参数要么是 dict 要么是 bytes, 所以你试试 encode 一下, 或者直接改用 json=dict 的方式, Requests 自动帮你 dumps 和 encode
    Colorful
        2
    Colorful  
    OP
       2020-05-16 17:22:06 +08:00
    @ClericPy 我不太知道怎么贴那块的报错信息
    python 版本是 3.7,你说的 curl 是网址?
    littleylv
        3
    littleylv  
       2020-05-16 17:23:37 +08:00
    requests.get(url='http://XXXXX',params=idsdata,headers=headersjson,)

    python 用的不多,盲猜一下
    get 用 params
    post 用 data
    另外不需要 json.dumps

    PS:
    遇到问题先看看手册你传参符不符合要求
    Colorful
        4
    Colorful  
    OP
       2020-05-16 17:26:28 +08:00
    @littleylv 好的,我去看看手册,感谢了
    littleylv
        5
    littleylv  
       2020-05-16 17:26:45 +08:00
    实测:

    Python 3.7.7 (default, Mar 10 2020, 15:43:33)
    [Clang 11.0.0 (clang-1100.0.33.17)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import requests
    >>> idsdata = {'id':100,'skiids':[1,2,3,7]}
    >>> res = requests.get('http://httpbin.org/get', params=idsdata)
    >>> res.json()
    {'args': {'id': '100', 'skiids': ['1', '2', '3', '7']}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.22.0', 'X-Amzn-Trace-Id': 'Root=1-5ebfb190-43a2c3f9e127ee27bad4de17'}, 'origin': 'xxx', 'url': 'http://httpbin.org/get?id=100&skiids=1&skiids=2&skiids=3&skiids=7'}
    >>> res = requests.get('http://httpbin.org/get', data=idsdata)
    >>> res.json()
    {'args': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '42', 'Content-Type': 'application/x-www-form-urlencoded', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.22.0', 'X-Amzn-Trace-Id': 'Root=1-5ebfb1a4-8c65a9f67209c70e1b262f85'}, 'origin': 'xxx', 'url': 'http://httpbin.org/get'}
    Colorful
        6
    Colorful  
    OP
       2020-05-16 17:50:05 +08:00
    @littleylv 有个比较奇怪的问题
    requests.get(url='http://XXXXX',params=idsdata,headers=headersjson,)



    改成这样之后,确实能够请求到接口,但是返回的数据不太对

    以这个数据为例:
    idsdata = {'id':100,'skiids':[1,2,3,7]}


    因为我看到浏览器里面请求是这样的
    id:100,
    skiids:1,
    skiids:2,
    skiids:3,
    skiids:7,

    正确应该是这样的
    skiids[]:1,
    skiids[]:2,
    skiids[]:3,
    skiids[]:7,
    Colorful
        7
    Colorful  
    OP
       2020-05-16 17:53:21 +08:00
    @littleylv 在传的时候直接这样写就可以了 skiids[],好奇怪
    crella
        8
    crella  
       2020-05-16 18:26:00 +08:00 via Android
    @Colorful curl 是指浏览器的开发人员工具,“网络”标签,选中请求,右键,复制为,curl(bash 形式)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1089 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 22:43 · PVG 06:43 · LAX 15:43 · JFK 18:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.