Python3 html获取

版权声明: https://blog.csdn.net/hhq2lcl/article/details/78652334
import urllib.request   #导入urllib库的request模块

#指定要抓取的网页url,必须以http开头的
url='http://blog.csdn.net/u014453898/article/details/54848707'
headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}


#调用 urlopen()从服务器获取网页响应(respone),其返回的响应是一个实例

respone=urllib.request.urlopen(url)


#调用返回响应示例中的read()函数,即可以读取html,但需要进行解码,
#具体解码写什么,要在你要爬取的网址右键,查看源代码;如‘utf-8’

html=respone.read().decode('utf-8')
'''# print(respone.info())——info()获取编码方式 #'''
urllib.request.urlretrieve(url,'./html_doc.txt')

猜你喜欢

转载自blog.csdn.net/hhq2lcl/article/details/78652334