如何使用BeautifulSoup从网站中提取“ href”链接?
BeautifulSoup是第三方Python库,用于解析网页中的数据。它有助于Web抓取,Web抓取是从不同资源提取,使用和处理数据的过程。
Web抓取还可以用于提取数据以用于研究目的,了解/比较市场趋势,执行SEO监视等等。
可以运行以下行在Windows上安装BeautifulSoup-
pip install beautifulsoup4
以下是一个例子-
示例
from bs4 import BeautifulSoup输出结果import requests
url = "https://en.wikipedia.org/wiki/Algorithm"
req = requests.get(url)
soup = BeautifulSoup(req.text, "html.parser")
print("href链接为:")
for link in soup.find_all('a'):
print(link.get('href'))
href链接为:…
https://stats.wikimedia.org/#/en.wikipedia.org
https://foundation.wikimedia.org/wiki/Cookie_statement
https://wikimediafoundation.org/
https://www.mediawiki.org/
说明
所需的软件包已导入并使用别名。
网站已定义。
网址已打开,并从中读取数据。
“ BeautifulSoup”功能用于从网页中提取文本。
'find_all'函数用于从网页数据中提取文本。
href链接打印在控制台上。
以上是 如何使用BeautifulSoup从网站中提取“ href”链接? 的全部内容, 来源链接: utcz.com/z/321385.html