import urllib.request
import http.cookiejar
def request(url,headers):
'''
两个参数
:param url:统一资源定位符,请求网址
:param headers:请求头
:return:html
'''
# ***************** Begin ******************** #
cookie = http.cookiejar.CookieJar()
handler = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)
r= opener.open(url)
# ***************** End ******************** #
html = r.read().decode('utf-8')
return html
头歌urllib爬虫-第一关
于 2022-04-19 17:01:16 首次发布
此篇博客介绍了如何使用Python的urllib和http.cookiejar库,通过设置Cookie处理HTTP请求,实现对指定URL的高效抓取。重点在于理解Cookie的工作原理和在实际项目中的应用。
摘要由CSDN通过智能技术生成