Python 如何使用Beautiful Soup在指定类中查找链接
我正在使用BeautifulSoup4解析一个新闻站点中包含在正文文本中的链接。我能够找到包含链接的所有段落,但每个链接的Python 如何使用Beautiful Soup在指定类中查找链接,python,beautifulsoup,Python,Beautifulsoup,我正在使用BeautifulSoup4解析一个新闻站点中包含在正文文本中的链接。我能够找到包含链接的所有段落,但每个链接的段落.get('href')返回类型none。我正在使用Python 3.5.1。非常感谢您的帮助 from bs4 import BeautifulSoup import urllib.request import re soup = BeautifulSoup("http://www.cnn.com/2016/11/18/opinions/how-do-you-deal
段落.get('href')
返回类型none
。我正在使用Python 3.5.1。非常感谢您的帮助
from bs4 import BeautifulSoup
import urllib.request
import re
soup = BeautifulSoup("http://www.cnn.com/2016/11/18/opinions/how-do-you-deal-with-donald-trump-dantonio/index.html", "html.parser")
for paragraph in soup.find_all("div", class_="zn-body__paragraph"):
print(paragraph.get('href'))
你真的想要这个吗
for paragraph in soup.find_all("div", class_="zn-body__paragraph"):
for a in paragraph("a"):
print(a.get('href'))
请注意,段落.get('href')
尝试在找到的
标记中查找属性href
。由于没有这样的属性,它返回None
。最可能的情况是,您实际上必须找到所有标记
,它们是您的
的后代(这可以通过段落(“a”)
来完成,这是段落的快捷方式。找到所有(“a”)
,然后查看每个元素的href
属性