import requests
from bs4 import BeautifulSoup
try :
r=requests.get("http://python123.io/ws/demo.html",timeout=30)
r.raise_for_status()
r.encoding=r.apparent_encoding
demo=r.text
soup=BeautifulSoup(demo,'html.parser')
#找到所有的a 标签
for link in soup.find_all("a"):
#打印键值对
print(link.get('href'))
except:
print('异常')爬取a 标签的连接
最新推荐文章于 2024-07-17 14:11:27 发布
本篇博客介绍了一种使用Python的requests库和BeautifulSoup库来抓取网页上的所有a标签链接的方法。通过设置超时时间和状态码检查确保了请求的成功率,并通过apparent_encoding属性正确解析了网页编码。
1665

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



