V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
mapnaqi
V2EX  ›  Python

scrapy post 怎么发送 json 数据

  •  
  •   mapnaqi · Feb 11, 2019 · 3991 views
    This topic created in 2635 days ago, the information mentioned may be changed or developed.

    需要带的数据为 data = {"a": [{"b": 'c'}]} 我用的这种写法一直不对, return scrapy.FormRequest( body=json.dumps(data), )

    12 replies    2019-02-15 00:26:12 +08:00
    butterfly1211
        1
    butterfly1211  
       Feb 11, 2019
    不需要 json.loads 直接传字典试试
    mapnaqi
        2
    mapnaqi  
    OP
       Feb 11, 2019
    试过, 不行, formdata 传字典, 或者 body 传 json 都是 状态码 408 的错误
    butterfly1211
        3
    butterfly1211  
       Feb 11, 2019
    可以试着传一下抓包 body 的截图,如果是"?1=2&3=4"这种的话需要可能需要换个思路
    mapnaqi
        4
    mapnaqi  
    OP
       Feb 11, 2019
    @butterfly1211 数据长这个样子, b 是一个编码, 是数字和英文的
    data = {"lineItems": [{"catalogNumber": b}]}
    用 requests 写没问题, 用了 scrapy 就是 408,
    butterfly1211
        5
    butterfly1211  
       Feb 11, 2019
    试一下,request = scrapy.Request( url, method='POST',
    body=json.dumps(my_data),)方式
    airect
        6
    airect  
       Feb 11, 2019   ❤️ 1
    body 里是 json 字符串,headers 中加 Content-Type: application/json
    mapnaqi
        7
    mapnaqi  
    OP
       Feb 11, 2019
    @butterfly1211 还是不行
    mapnaqi
        8
    mapnaqi  
    OP
       Feb 11, 2019
    408 Request Time-out
    butterfly1211
        9
    butterfly1211  
       Feb 11, 2019   ❤️ 1
    FormRequest 中 {‘ key': ‘ value', ‘ k': ‘ v'}会被转化为'key=value&k=v' 并且默认的 method 是 POST,你可以再看一下 body 的原始格式,看是这种 key-value 的形式还是 json 格式在作分析
    cz5424
        10
    cz5424  
       Feb 12, 2019 via iPhone
    @mapnaqi 这个提示并不是请求格式的问题吧,自己抓自己包看看
    encro
        11
    encro  
       Feb 12, 2019
    抓包,或者写一个测试用例接受请求过来的 body 和 head,比较有什么不同,实在不行用 request 替代好了。最新的 request-html 已经自带 html 解析,满足大部分爬虫场景。
    luoleng
        12
    luoleng  
       Feb 15, 2019
    formdata=data
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5394 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 46ms · UTC 07:33 · PVG 15:33 · LAX 00:33 · JFK 03:33
    ♥ Do have faith in what you're doing.