学习爬虫记笔记实例:
#!/usr/bin/env python3
#coding:utf-8
import urllib.request
import urllib.parse
url ="http://www.baidu.com"
res = urllib.request.urlopen(url)
data = res.read()
# print(data)
#保存data到内存
with open(r'/Users/dinglp/Desktop/file.html','wb')as f:
f.write(data)
#保存html文件到内存
urllib.request.urlretrieve(url,filename="/Users/dinglp/Desktop/file1.html")
# print(type(data))
# data = res.readline()
# data = res.readlines()
print(res.info())
print(res.getcode())
print(res.geturl())
#如果有url有中文或者%E1%2S用 urllib.request.quote(url) urllib.request.unquote(url)