【发布时间】:2017-10-04 12:20:53
【问题描述】:
我有以下几点:
html =
'''<div class=“file-one”>
<a href=“/file-one/additional” class=“file-link">
<h3 class=“file-name”>File One</h3>
</a>
<div class=“location”>
Down
</div>
</div>'''
并且只想获取href 的文本,即/file-one/additional。所以我做了:
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
link_text = “”
for a in soup.find_all(‘a’, href=True, text=True):
link_text = a[‘href’]
print “Link: “ + link_text
但它只是打印一个空白,什么都没有。只需Link:。所以我在另一个网站上测试了它,但使用了不同的 HTML,它工作正常。
我做错了什么?还是该站点有可能故意编程为不返回href?
提前感谢您,一定会支持/接受答案!
【问题讨论】:
-
你的 HTML 中真的有花引号吗?
-
就此而言,为什么您的 代码 中有弯引号?你在编码什么?您需要使用文本编辑器。
-
如果您删除参数
text=True,您的代码对我有效 -
如果您需要更多关于报价的信息,请查看这篇文章:blogs.msdn.microsoft.com/oldnewthing/20090225-00/?p=19033
-
@downshift
text=True做什么?以为它以文本形式返回
标签: python html web-scraping beautifulsoup