Python 爬虫返回加密数据\x

2022-09-12 14:27:34 +08:00
 zzzkkk
很简单的代码
if __name__ == '__main__':
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:104.0) Gecko/20100101 Firefox/104.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
"Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive",
"Upgrade-Insecure-Requests": "1",
"Sec-Fetch-Dest": "iframe",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "same-site"
}
import urllib.request
req = urllib.request.Request("https://www.ebay.com", headers = headers)
resp = urllib.request.urlopen(req)
print(resp.read())

返回的都是 xfb\xb9\xbd\x8e\x92Q\xda\xbf\x1b\x9b\xe2i\xc7\xed>n\x
1698 次点击
所在节点    Python
4 条回复
Alexonx
2022-09-12 14:45:57 +08:00
这不是加密 这是 GZip 压缩了 把 Accept-Encoding 里面的 gzip 去了试试
learningman
2022-09-12 15:14:15 +08:00
新手换 requests
filwaline
2022-09-12 15:15:00 +08:00
@Alexonx
正解,或者是加行代码来解压

```
import gzip
content = gzip.decompress(resp.read())
```
HankLu
2022-09-12 16:00:52 +08:00
requests 不香吗?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/879435

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX