from urllib.request import urlopen
url = 'http://www.baidu.com'
resp = urlopen(url)
print(resp.read().decode('utf-8'))
with open('e://mypa.html', mode='w') as f:
f.write(resp.read().decode('utf-8'))
print('over')
写入文件地址多写了一个 /
应该是'e:/mypa.html'
再有。resp.read().decode('utf-8')是读取全部数据,只能读取一次,读取之后读取的指针到了数据的结束位置。
第二次读取是从上次读取指针的位置开始。自然就是空了
正确的代码如下:(如有帮助,望采纳!谢谢! 点击我这个回答右上方的【采纳】按钮)
from urllib.request import urlopen
url = 'http://www.baidu.com'
resp = urlopen(url)
s = resp.read().decode('utf-8')
print(s)
with open('e:/mypa.html', mode='w',encoding='utf-8') as f:
f.write(s)
print('over')
将代码中这句with open('e://mypa.html', mode='w') as f:双斜杠去掉一个即可。
什么牛马操作,把python学熟练了再学爬虫吧
正反斜杠要不要转义都搞不清啊
from urllib.request import urlopen
url = 'http://www.baidu.com'
resp = urlopen(url)
print(resp.read())
with open('d://mypa.html', mode='wb') as f:
f.write(resp.read())
print('over')