Python-Http请求

使用
httpget.py

#!usr/bin/evn python3
# -*- coding: utf-8 -*-

# 引用库,Python3使用urllib.request
import urllib.request

# ----Get请求
url = 'http://www.baidu.com'
# response = urllib.request.urlopen(url)

# 带头的请求, 需要构造Reqeust对象来设置
request = urllib.request.Request(url, headers = {'User-Agent' : 'Python3/xzm'})
response = urllib.request.urlopen(request)

# 返回数据是utf-8编码,进行转码。Python的字符串是使用unicode编码
print(response.read().decode('utf-8'))
python httpget.py

参考文章:
python3网络爬虫一《使用urllib.request发送请求》
https://blog.csdn.net/bo_mask/article/details/76067790

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容