欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

Pythonurllib模块

时间:2023-05-18

       该模块提供了get请求和post请求,下面是获取百度请求的网页源码,仅供参考

        下面是我导入的urllib模块

from urllib import request
        下面是请求携带的头部,其中最重要的是User-Agent,该头部的功能是模拟浏览器像网站发起请求,其他的可有可无,头部信息可以根据抓包工具或者浏览器的开发者工具查看,具体请自行百度

header={"Accept": "application/json, text/plain, **","User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36 Edg/98.0.1108.43","Content-Type": "application/json;charset=UTF-8","Origin": "http://xiaobei.dalaola.com","Referer": "http://xiaobei.dalaola.com/user","Accept-Encoding": "gzip, deflate","Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6","cookie": "AOAOSTAR_SESSID=c94a1d760ba93dc0e79aad1b9e03dd47"}req=request.Request("https://www.baidu.com/",method="get",headers=header)res=request.urlopen(req)htmls = res.read()buff = io.BytesIO(htmls)f = gzip.GzipFile(fileobj=buff)htmls = f.read().decode('utf-8')print(htmls)

控制台运行结果如下:

 

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。