python爬虫第1练url lib

2021/5/3 12:25:25

本文主要是介绍python爬虫第1练url lib,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

'''本代码在mac中测试通过‘’‘

#也可以使用import urllib.request,调用方法时需加上urllib,即以urllib.request开头
from urllib import request
#本部分尚不理解,待日后研究
import ssl
ssl._create_default_https_context = ssl._create_unverified_context

#以上部分可能受ssl验证机制影响,据同学讲在windows下不需要,未实测

#为保密原因,以***隐去部分字符
url='https://www.***.com'

headers={'User-Agent':' Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) ********'}

'创建对象'
req= request.Request(url, headers=headers)
'获取响应'
res = request.urlopen(req)
'读取网页'
html=res.read().decode('utf-8')
'输出结果'
# print(html)
with open('firsthtml.html','w',encoding='utf-8') as file_obj:
    file_obj.write(html)


这篇关于python爬虫第1练url lib的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程