from urllib import request
import re
page=54 #匹配网站
url="正在上传…重新上传取消 网页链接+str(page)
try:
#请求头
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36 Edg/99.0.1150.46'}
#定义请求,传入请求头
req=request.Request(url,headers=headers)
#打开网页
resp=request.urlopen(req)
#打印返回信息 解码
content=resp.read().decode('utf-8')
print(content)
pattern=re.compile(r'(>*?)')
#匹配html 找到所有的a链接标题和内容
items=re.findall(pattern,content)
for i in items:
print(i[0]+'\t'+i[1])
except request.URLError as e:
if hasattr(e,'code'):
print(e.code)
if hasattr(e,'reason'):
print(e.reason)
网络爬虫 爬取a链接——梁鑫
最新推荐文章于 2023-07-28 00:00:36 发布