Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Beautifulsoup通过文本获取Href_Python_Beautifulsoup - Fatal编程技术网

Python 使用Beautifulsoup通过文本获取Href

Python 使用Beautifulsoup通过文本获取Href,python,beautifulsoup,Python,Beautifulsoup,我正在使用“requests”和“beautifulsoup”从带有特定文本的网页中搜索所有href链接。我已经做了,但是如果文本换了一行,beautifulsoup不会“看到”它,也不会返回该链接 soup = BeautifulSoup(webpageAdress, "lxml") path = soup.findAll('a', href=True, text="Something3") print(path) 例如: 如下所示,它返回Something3文本的Href: ... &

我正在使用“requests”和“beautifulsoup”从带有特定文本的网页中搜索所有href链接。我已经做了,但是如果文本换了一行,beautifulsoup不会“看到”它,也不会返回该链接

soup = BeautifulSoup(webpageAdress, "lxml")

path = soup.findAll('a', href=True, text="Something3")
print(path)

例如:

如下所示,它返回Something3文本的Href:

...
<a href="page1/somethingC.aspx">Something3</a>
...
...
<a href="page1/somethingC.aspx">
Something3</a>
...
。。。
...
这样,它不会返回Something3文本的Href:

...
<a href="page1/somethingC.aspx">Something3</a>
...
...
<a href="page1/somethingC.aspx">
Something3</a>
...
。。。
...
不同之处在于Href text(Something3)位于新行中。 我不能更改HTML代码,因为我不是那个网页的站长

你知道我该怎么解决吗


注意:我已经尝试使用soup.replace('\n','').replace('\r','')但我得到错误:NoneType对象不可调用。

您可以使用正则表达式查找任何包含“Something3”的文本:

html=''
'''
从bs4导入BeautifulSoup
进口稀土
soup=BeautifulSoup(html,“lxml”)
path=soup.findAll('a',href=True,text=re.compile(“Something3”))
对于路径中的链接:
打印(链接['href'])

您可以使用正则表达式查找任何包含“Something3”的文本:

html=''
'''
从bs4导入BeautifulSoup
进口稀土
soup=BeautifulSoup(html,“lxml”)
path=soup.findAll('a',href=True,text=re.compile(“Something3”))
对于路径中的链接:
打印(链接['href'])

和不带正则表达式的解决方案:

path = soup.select('a')
if path[0].getText().strip() == 'Something3':
print(path)
输出:

[<a href="page1/somethingC.aspx">
Something3</a>]
[]

和不带正则表达式的解决方案:

path = soup.select('a')
if path[0].getText().strip() == 'Something3':
print(path)
输出:

[<a href="page1/somethingC.aspx">
Something3</a>]
[]

您可以在bs4.7.1中使用
:包含
伪类

from bs4 import BeautifulSoup as bs

html = '<a href="page1/somethingC.aspx">Something3</a>'
soup = bs(html, 'lxml')
links = [link.text for link in soup.select('a:contains(Something3)')]
print(links)
从bs4导入美化组作为bs
html=“”
soup=bs(html,“lxml”)
links=[link.text表示汤中的链接。选择('a:contains(Something3)]
打印(链接)

您可以在bs4.7.1中使用
:包含
伪类

from bs4 import BeautifulSoup as bs

html = '<a href="page1/somethingC.aspx">Something3</a>'
soup = bs(html, 'lxml')
links = [link.text for link in soup.select('a:contains(Something3)')]
print(links)
从bs4导入美化组作为bs
html=“”
soup=bs(html,“lxml”)
links=[link.text表示汤中的链接。选择('a:contains(Something3)]
打印(链接)

re.compile的作用是什么?请阅读更多信息。但基本上允许匹配/查找模式,而不是只查找关键字。如果我们做了
text=“Something3”
,那么
\n
将不会返回任何内容,因为它不准确。因此,我们更愿意寻找子字符串是否在整个字符串中。正则表达式只是一种方法,谢谢你的解释!re.compile做什么?阅读更多有关它的信息。但基本上允许匹配/查找模式,而不是只查找关键字。如果我们做了
text=“Something3”
,那么
\n
将不会返回任何内容,因为它不准确。因此,我们更愿意寻找子字符串是否在整个字符串中。正则表达式只是一种方法,谢谢你的解释!谢谢你们的帮助回答谢谢你们的帮助回答谢谢你们的回答。你帮了我很多!:)谢谢大家的回答。你帮了我很多!:)啃老族把戏!现在我必须学习如何使用伪类进行选择…@JackFleeting是的。我对新的bs4非常满意。它还有很多很棒的功能,我不知道。谢谢!;)别担心。我希望它比正则表达式快,但还没有测试过。Neet技巧!现在我必须学习如何使用伪类进行选择…@JackFleeting是的。我对新的bs4非常满意。它还有很多很棒的功能,我不知道。谢谢!;)别担心。我希望它比正则表达式更快,但还没有测试过。