|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这是爬虫学习的第一课 来爬百度首页
from urllib.request import urlopen
#输入网址
url = "http://www.baidu.com"
#获取内容
resp = urlopen(url)
#读取内容
resp.read().decode("utf-8")
#保存到文件里面去
with open("mybaidu.html", mode="w") as f:
f.write(str(resp.read().decode("utf-8")))
print("over!")
为什么最后保存的文件里面是空白的呢?
救救孩子吧,莫要让一个有着大好前途的青年折戟于爬虫学习的第一课!!
- from urllib import request
- headers = {'User-Agent': 'Firefox'}
- req = request.Request('http://www.baidu.com', headers=headers)
- r = request.urlopen(req)
- txt = r.read().decode('utf-8')
- print(txt)
- with open('test.txt', 'w') as f:
- f.write(txt)
复制代码
|
|