如何选择 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/