python - 如何使用 BeautifulSoup 选择 href 类标签?

标签 python beautifulsoup python-requests

如何选择 href 类标签?

html 代码示例:

<a title="bla" class="example"> text </a>

所以我希望通过“title”或“class”确定要从哪个标签获取,然后输出 a 标签内的文本,因此在这种情况下输出将为

text

我正在使用的代码

from bs4 import BeautifulSoup
import requests

source = requests.get('http://www.example.com').text
soup = BeautifulSoup(source, 'lxml')

for profile in soup.select(" select input here "):

    print(profile.text.encode("utf-8"))

最佳答案

除了 @Stack 在评论中建议的内容之外:

soup.find_all('a', {'title': 'bla'})
soup.find_all('a', {'class': 'example'})

您可以使用 CSS selectors 来做到这一点(我什至看到您已经在那里进行了 select() 调用:

soup.select("a[title=bla]")
soup.select("a.example")

关于python - 如何使用 BeautifulSoup 选择 href 类标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48176990/

相关文章:

python - 向系列添加元素时出现问题

python - Pandas.read_csv() 在列名中带有特殊字符(重音符号)

python - 在 3D 空间中插值标量场

python - 将标题添加到我已抓取的表格中

python - 脚本中的 UTF 8 不匹配

python - 使用 Pandas 将列值聚合到序列中

python - 使用 beautifulsoup 从 craigslist 获取价格

python - 使用 Python BeautifulSoup 提取 HTML 表

python - 两个类在 HTML 中具有相同的名称,BeautifulSoup 仅选择第一个类

python - 如何替换python中特定单词下的值?