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
woshichuanqilz
V2EX  ›  Python

几乎一样的代码和配置, aiohttp 和 requests 获取到的页面内容不一样, 这个问题可能出在哪?

  •  
  •   woshichuanqilz · Aug 2, 2020 · 4029 views
    This topic created in 2096 days ago, the information mentioned may be changed or developed.

    查了一下 Stack Overflow 问题可能在 ssl 上, 但是改了还是不行。

    import asyncio
    import requests
    import time
    import json
    import random
    import aiohttp
    from ori_async import get_proxy
    
    list_url = 'https://www.amazon.co.uk/s?k=dress&ref=nb_sb_noss'
    product_url = 'https://www.amazon.co.uk/AUSELILY-Womens-Sleeve-Pleated-Pockets/dp/B082W811L3/ref=sr_1_1_sspa?dchild=1&keywords=dress&qid=1596264150&s=clothing&sr=1-1-spons&psc=1&spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUEzTEpRR0NLRlhQMFFDJmVuY3J5cHRlZElkPUEwMDY5Nzg5MkZTUllZWTM3VFVIQiZlbmNyeXB0ZWRBZElkPUEwOTU0NzQ1MTE0QzhFV0w0SjJOMCZ3aWRnZXROYW1lPXNwX2F0ZiZhY3Rpb249Y2xpY2tSZWRpcmVjdCZkb05vdExvZ0NsaWNrPXRydWU='
    baidu_url = 'https://www.baidu.com'
    
    
    headers = {
            "authority": "www.amazon.co.uk",
            "method": "GET",
            "path": "/s?k=dress&i=clothing&ref=nb_sb_noss",
            "scheme": "https",
            "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
            "accept-encoding": "gzip, deflate, br",
            "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-US;q=0.7,ja;q=0.6,zh-TW;q=0.5",
            "cache-control": "max-age=0",
            "cookie": "session-id=258-7467381-0898317; i18n-prefs=GBP; ubid-acbuk=262-6045121-6110346; x-wl-uid=13CgqXYwZCFN3okL9HYQm7Iyt8Md2S5Dj4uA/dwKZrgtM0V8Ii0/OTO/AbtTOSRVzmlpbyCfIvZw=; session-token=\"K1UgAkfjQLKORhX6CN0AXXcRycoGecW5zqC3Nk8y/0rr7ZHQPpm5kzjT0YPAY/M8vVOWYNXoWCDYhSbrXyHP/bnNILWcLd2I+04dfUASNtpMHQkTh2YNffN748Rd9HxMQ6wFjVkDsfhgbm/YgEQ5uy8H+qWM1i9z+8uePKVL1BfVQtQpOEFxLQK+1GRQHBfdt7urN81Bkg0WjHz3pOVR31pILIjf3aM1nhncWG1P/A93yPpUOBTFeMh5/6V4RrddfHGn4uifWdg=\"; session-id-time=2082758401l; csm-hit=tb:3GP38DER2C68WEEPMTK8+s-KCAYBB85E78WBDWP92X7|1595834228498&t:1595834228498&adb:adblk_yes",
            "downlink": "9.75",
            "ect": "4g",
            "rtt": "200",
            "sec-fetch-dest": "document",
            "sec-fetch-mode": "navigate",
            "sec-fetch-site": "same-origin",
            "sec-fetch-user": "?1",
            "upgrade-insecure-requests": "1",
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4181.9 Safari/537.36"
    }
    url = list_url
    # proxies = get_proxy()
    async def main():
        connector = aiohttp.TCPConnector(ssl=False)
        async with aiohttp.ClientSession(connector=connector) as session:
            async with session.get(url, headers=data["headers"]) as resp:
                print(resp.status)
                content = await resp.text()
                print(len(content))
    
    start = time.time()
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
    end = time.time()
    print('spend time is {}'.format(end - start))
    
    # response = requests.get(url, headers=headers, proxies=proxies, timeout=8, verify=False)
    response = requests.get(url, headers=headers, timeout=8, verify=False)
    print("length is {}".format(len(response.text)))
    
    
    13 replies    2020-08-05 17:09:05 +08:00
    locoz
        1
    locoz  
       Aug 2, 2020
    抓包对比,分分钟就能看到问题所在
    woshichuanqilz
        2
    woshichuanqilz  
    OP
       Aug 2, 2020
    @locoz
    你好我用了 fiddler 抓包 但是 aiohttp 的请求在 fiddler 里面不显示。

    requests 是正常的。

    aiohttp 的抓取过程是有的 有页面保存下来
    picone
        3
    picone  
       Aug 2, 2020
    wireshark 抓包对比下,可能底层做了些奇怪的处理。
    Trim21
        4
    Trim21  
       Aug 2, 2020 via Android
    aiohttp 不能设置 proxy 吗…
    gesse
        5
    gesse  
       Aug 2, 2020
    电商网站, 不一样的用户指纹,返回不一样的内容不很正常吗?
    woshichuanqilz
        6
    woshichuanqilz  
    OP
       Aug 2, 2020 via Android
    @gesse 两个方法用的信息基本是一样的 你说的指纹具体指的是什么这个例子能修改吗
    woshichuanqilz
        7
    woshichuanqilz  
    OP
       Aug 2, 2020 via Android
    @Trim21 可以但是这个是是代理的问题吗
    Trim21
        8
    Trim21  
       Aug 2, 2020 via Android
    @woshichuanqilz 我是说 fiddler 抓包的问题
    Nigelchiang
        9
    Nigelchiang  
       Aug 2, 2020
    楼主你的 cookie 都泄露了,赶紧把亚马逊退出重登一下吧
    levelworm
        10
    levelworm  
       Aug 2, 2020
    cookie 建议不要贴出来。
    gesse
        11
    gesse  
       Aug 3, 2020
    @woshichuanqilz

    https://fingerprintjs.com/
    你自己用浏览器访问, 清缓存、清 cookie 前后对比, 看那串红色的数字是不是一样的。
    locoz
        12
    locoz  
       Aug 3, 2020
    @woshichuanqilz #2
    aiohttp 的请求在抓包工具里不显示大概是因为你根本没有设置让它走代理,在请求的时候带上 proxy 参数就行了。

    抓包先看 HTTP 层的各个参数有没有区别,有的话先处理成一模一样的。aiohttp 和 requests 的实现不同,对一些情况的处理并不相同,比如常见容易出问题的情况是 headers 里多了东西或者少了东西,或者是某个有特殊符号之类的 value 被编码成另一种样子之类的。
    如果 HTTP 层都统一了但还是有区别的话,就用 wireshark 看看 SSL 层有没有什么区别,比如常见用来做 SSL 指纹的加密套件部分。
    Te11UA
        13
    Te11UA  
       Aug 5, 2020
    不一样是什么意思,长度不一样还是验证码?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3174 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 11:49 · PVG 19:49 · LAX 04:49 · JFK 07:49
    ♥ Do have faith in what you're doing.