python之get/post请求指定URL返回的网页内容,出现gzip乱码解决

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2021/3/9
# @Author :
# @Platform:


import json
from common.proxyPool import get_proxy2
import requests
import warnings
warnings.filterwarnings("ignore")

url = "https://api3-normal-c-hl.amemv.com/aweme/v1/promotion/user/promotion/list/?user_id=59953478993&sec_user_id=MS4wLjABAAAA7VMnAHca11ga-p6PwQkrj2Rir2JnBwbQbXMEU___vJY&cursor=20&count=20&column_id=0&goods_type=2&shop_version=1&os_api=23&device_type=Nexus%206&ssmix=a&manifest_version_code=110301&dpi=560&app_name=aweme&version_name=11.3.0&ts=1615259629&cpu_support64=false&app_type=normal&ac=wifi&host_abi=armeabi-v7a&update_version_code=11309900&channel=tengxun_new&_rticket=1615259631348&device_platform=android&iid=246721721152718&version_code=110300&mac_address=9C%3AD9%3A17%3A61%3AC4%3A46&cdid=1923777e-33c6-4410-94cb-5234df99b0a7&openudid=2ce10d59284c091b&device_id=70057154684&resolution=1440*2392&os_version=6.0.1&language=zh&device_brand=google&aid=1128"

headers ={
    "Host": "api3-normal-c-hl.amemv.com",
    "Connection": "keep-alive",
  "Cookie":"odin_tt=b83a8b1dfa0132db8431d18436e89320ed3b734cc54d46c3cd055f353bea3f6450984c899e15ff1b10fd2955e4ada3554cad0d36c4e821c603fccf437bbfb23c; install_id=246721721152718; ttreq=1$a225216c2d5ee0de39c81ff415847d4bb09e66ea",
    "X-SS-REQ-TICKET": "1615257861321",
    "sdk-version": "1",
    "X-SS-DP": "1128",
    "x-tt-trace-id": "00-14fa00770a104fbb587c7b1668c90468-14fa00770a104fbb-01",
    "User-Agent": "com.ss.android.ugc.aweme/110301 (Linux; U; Android 6.0.1; zh_CN; Nexus 6; Build/MOB31S; Cronet/TTNetVersion:b4d74d15 2020-04-23 QuicVersion:0144d358 2020-03-24)",
    "Accept-Encoding": "gzip, deflate, br",
    "X-Gorgon": "040408f300055339867837e83551bb5505c4ac74e06adca5e0a3",
    "X-Khronos": "1615259631",

}

response = requests.get(url, headers=headers,  verify=False,  timeout=5)
print("===",response.text)

两种方法获取正确页面返回内容

1.Header中把Accept-Encoding注释掉

注释此行
//"Accept-Encoding": "gzip, deflate, br",

//req.Headers.Add("Accept-Encoding", "gzip,deflate");

注释后返回结果:

2.设置Accept-Encoding的Header,同时设置对应的自动解压缩的模式

req.Headers["Accept-Encoding"] = "gzip,deflate";
req.AutomaticDecompression = DecompressionMethods.GZip;
具体采用哪种方法,自己根据需要选择

posted @ 2021-03-09 14:32  莫贞俊晗  阅读(796)  评论(0编辑  收藏  举报