python爬取新浪首页程序
import urllib.request
url = "http://www.sina.com.cn"
# 发送请求
response = urllib.request.urlopen(url)
# 读取数据
data = response.read()
print(data)
# with open("sina.html","wb") as f:
# f.write(data)
# print("新浪页面采集完成")
# 如果对数据进行操作,要转成字符串操作
html = data.decode("utf-8")
print(html)
with open("sina2.html","w",encoding="utf-8") as f:
f.write(html)