python – 如何在BeautifulSoup中获取搜索的上下文?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 如何在BeautifulSoup中获取搜索的上下文?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2717字,纯文字阅读大概需要4分钟。
内容图文
![python – 如何在BeautifulSoup中获取搜索的上下文?](/upload/InfoBanner/zyjiaocheng/787/92e8c81ad11d42d48e44c6fa091047ca.jpg)
我正在解析由各种HTML实体组成的网页,其中包括以下片段:
<p style="text-align: center;"><img src="http://example.com/smthg.png" alt="thealtttext" /></p>
<p style="text-align: center;"><strong>My keywords : <a href="http://example.com/hello.html" target="_blank"> some text </a> </strong></p>
<p style="text-align: center;"><strong>some other words : <a href="http://example.com/anotherlink.html" target="_blank"> some other words</a></strong></p>
我对我的关键字后面的网址感兴趣(上例中的http://example.com/hello.html).之后我的关键字和链接的组合在页面中是唯一的.
现在我使用正则表达式来提取URL:
import requests
import re
def getfile(link):
r = requests.get(link).text
try:
link = re.search('My keyword : <a href="(.+)" target', r).group(1)
except AttributeError:
print("no direct link for {link}".format(link=link))
else:
return link
print(getfile('http://example.com'))
这种方法在工作时非常依赖于匹配字符串的确切格式.我非常希望使用BeautifulSoup来:
>搜索我的关键字
>得到它的上下文(我的意思是包含该字符串的标记的整个值,我的关键字:< a href =“http://example.com/hello.html”target =“_ blank”> some text < / a>在上面的情况下)
>通过BeautifulSoup再次运行它,以便在< a>中提取URL.
我在第二部分失败了,我只能得到
[u'My keywords : ']
在尝试字符串搜索时
import bs4
import re
thehtml = '''
<p style="text-align: center;"><img src="http://example.com/smthg.png" alt="thealtttext" /></p>
<p style="text-align: center;"><strong>My keywords : <a href="http://example.com/hello.html" target="_blank"> some text </a> </strong></p>
<p style="text-align: center;"><strong>some other words : <a href="http://example.com/anotherlink.html" target="_blank"> some other words</a></strong></p>
'''
soup = bs4.BeautifulSoup(thehtml)
k = soup.find_all(text=re.compile("My keywords"))
print(k)
如何获取周围标签的全部内容? (我不能假设这总是< strong>如上例所示)
解决方法:
您可以使用find()代替find_all(),因为只有一个匹配项.然后使用next_sibling找到< a> tag和href来获取它的值,例如:
import bs4
import re
thehtml = '''
<p style="text-align: center;"><img src="http://example.com/smthg.png" alt="thealtttext" /></p>
<p style="text-align: center;"><strong>My keywords : <a href="http://example.com/hello.html" target="_blank"> some text </a> </strong></p>
<p style="text-align: center;"><strong>some other words : <a href="http://example.com/anotherlink.html" target="_blank"> some other words</a></strong></p>
'''
soup = bs4.BeautifulSoup(thehtml)
k = soup.find(text=re.compile("My keywords")).next_sibling['href']
print(k)
收益率:
http://example.com/hello.html
更新:基于注释,要获取包含某些文本的元素,请使用parent,如:
k = soup.find(text=re.compile("My keywords")).parent.text
产量:
<strong>My keywords : <a href="http://example.com/hello.html" target="_blank"> some text </a> </strong>
内容总结
以上是互联网集市为您收集整理的python – 如何在BeautifulSoup中获取搜索的上下文?全部内容,希望文章能够帮你解决python – 如何在BeautifulSoup中获取搜索的上下文?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。