【发布时间】:2021-01-25 11:26:43
【问题描述】:
我正在尝试弄清楚如何从给定站点中仅提取带有特定文本的链接
这是我使用的程序:
from BeautifulSoup import BeautifulSoup
import urllib2
import re
html_page = urllib2.urlopen("https://www.example.net/")
soup = BeautifulSoup(html_page)
linkContent = "Tartan Flannel Shirt "
for link in soup.findAll('a'):
print link.get('href')
Html链接就是这样的
<a class="name-link" href="/shop/all/shirts">Tartan Flannel Shirt </a>
如果我运行上面的程序,输出是网站中每个链接的列表,但我希望它只显示带有 Tartan Flannel Shirt 的链接。
【问题讨论】:
-
值得注意的是,您缺少右引号 - 因此您的脚本中存在语法错误
-
使用 python 2 ?
-
@PatrickArtner 你建议使用 Python3 吗?
-
@AleksJ 谢谢我没注意到
-
如果你使用它,我建议标记
python 2.x- 它已经死了,python 3.9 潜伏着所以你很落后。
标签: python beautifulsoup urllib2