Python 为什么';bs4是否找到href属性?
所以我正在学习使用atbwp,我现在正在做一个程序,在这个程序中,我在一个网站上打开前5个搜索结果。 这一切都很有效,直到我必须获得每个顶级结果的href并打开它。我得到这个错误:Python 为什么';bs4是否找到href属性?,python,html,beautifulsoup,href,Python,Html,Beautifulsoup,Href,所以我正在学习使用atbwp,我现在正在做一个程序,在这个程序中,我在一个网站上打开前5个搜索结果。 这一切都很有效,直到我必须获得每个顶级结果的href并打开它。我得到这个错误: Traceback (most recent call last): File "C:\Users\Asus\Desktop\pyhton\projects\emagSEARCH.py", line 33, in <module> webbrowser.open(url)
Traceback (most recent call last):
File "C:\Users\Asus\Desktop\pyhton\projects\emagSEARCH.py", line 33, in <module>
webbrowser.open(url)
File "C:\Users\Asus\AppData\Local\Programs\Python\Python38-32\lib\webbrowser.py", line 86, in open
if browser.open(url, new, autoraise):
File "C:\Users\Asus\AppData\Local\Programs\Python\Python38-32\lib\webbrowser.py", line 603, in open
os.startfile(url)
TypeError: startfile: filepath should be string, bytes or os.PathLike, not NoneType
它做它应该做的事情,直到for循环结束。它看起来很像书中的示例程序,所以我不明白为什么它不起作用。我做错了什么?如果您想提取
a
标记下的href
,请使用以下命令:
html = '<a href="https://comenzi.farmaciatei.ro/ingrijire-personala/ingrijire-corp-si-fata/tratamente-/the-no-brainer-set-the-ordinary-deciem-p344003"> The No-Brainer Set The Ordinary, Deciem</a>'
Soup=bs4.BeautifulSoup(html,'html.parser')
url = Soup.find('a')['href']
print(url)
webbrowser.open(url)
您可以对所有
a
标记执行相同操作,以获取所有hrefs
如果您想提取a
标记下的href
,请使用以下方法:
html = '<a href="https://comenzi.farmaciatei.ro/ingrijire-personala/ingrijire-corp-si-fata/tratamente-/the-no-brainer-set-the-ordinary-deciem-p344003"> The No-Brainer Set The Ordinary, Deciem</a>'
Soup=bs4.BeautifulSoup(html,'html.parser')
url = Soup.find('a')['href']
print(url)
webbrowser.open(url)
您可以对所有
a
标记执行相同操作,以获取所有hrefs
您可以提供完整的代码吗?您的问题是什么?是否要提取a标记中的href?这就是你想做的吗?你能提供完整的代码吗?你的问题是什么?是否要提取a标记中的href?这就是你想做的吗?
html = '<a href="https://comenzi.farmaciatei.ro/ingrijire-personala/ingrijire-corp-si-fata/tratamente-/the-no-brainer-set-the-ordinary-deciem-p344003"> The No-Brainer Set The Ordinary, Deciem</a>'
Soup=bs4.BeautifulSoup(html,'html.parser')
url = Soup.find('a')['href']
print(url)
webbrowser.open(url)
https://comenzi.farmaciatei.ro/ingrijire-personala/ingrijire-corp-si-fata/tratamente-/the-no-brainer-set-the-ordinary-deciem-p344003