from urllib.request import urlopen
url="http://www.baidu.com/"
response=urlopen(url)#得到这个网址的响应
#response.read() #这个代表从这个响应里读取内容,没有任何输出
#print(response.read()) 这样写是错误的,输出b‘,意思是字节,需要更改解码方式
#response.read().decode("utf-8")
with open("mybaidu.html",mode="w",encoding='utf-8')as f:
b=response.read().decode("utf-8") #decode可以转换编码方式
f.write(b) #将网页中读取的内容写入到mybaidu里
print("over~")
爬取百度首页不报错但内容为空的情况
最新推荐文章于 2023-05-03 10:01:32 发布