'''本代码在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
最新推荐文章于 2026-06-24 09:50:49 发布
本文介绍了一种在Mac环境中使用Python的urllib库抓取网页的方法,包括处理HTTPS请求和设置请求头来模拟浏览器访问。

2172

被折叠的 条评论
为什么被折叠?



