urllib网络请求是python自带的
import urllib
#请求
from urllib import request
if name == ‘main’:
# 服务器响应
response1 = urllib.request.urlopen(url=“http://www.baidu.com”)
text = response1.read().decode(“utf-8”)
with open(‘baidu.html’, mode=‘w’, encoding=‘utf-8’) as fp:
fp.write(text)
print(‘文本数据已写入’)
picture='https://tse4-mm.cn.bing.net/th/id/OIP.F64SlQFR9lbjnlDYm4ojOAHaEo?pid=Api&rs=1'
response = urllib.request.urlopen(url=picture)
text = response.read()
with open('picture.jpg', mode='wb') as fp:# 二进制是最原始的数据,无需进行编码
fp.write(text)
print('图片数据已写入')