使用BeautifulSoup搜索字符串的html [英] Using BeautifulSoup to search html for string

查看:186
本文介绍了使用BeautifulSoup搜索字符串的html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用BeautifulSoup来查找特定页面上的用户输入的字符串。
例如,我想看看字符串'Python的位于页面上: http://python.org

I am using BeautifulSoup to look for user entered strings on a specific page. For example, I want to see if the string 'Python' is located on the page: http://python.org

当我使用:
find_string = soup.body.findAll(文='的Python')
find_string返回 []

但是,当我使用:
find_string = soup.body.findAll(文= re.compile('Python的),上限= 1)
find_string返回 [u'Python乔布斯'] 如预期

But when I used: find_string = soup.body.findAll(text=re.compile('Python'), limit=1) find_string returned [u'Python Jobs'] as expected

是什么使得要搜索的字的多个实例时,有第二条语句工作这两个语句之间的差

What is the difference between these two statements that makes the second statement work when there are more than one instances of the word to be searched

推荐答案

下面一行正在寻找的精确 NavigableString'Python的:

The following line is looking for the exact NavigableString 'Python':

>>> soup.body.findAll(text='Python')
[]

请注意,以下NavigableString发现:

Note that the following NavigableString is found:

>>> soup.body.findAll(text='Python Jobs') 
[u'Python Jobs']

请注意此行为:

>>> import re
>>> soup.body.findAll(text=re.compile('^Python$'))
[]

所以,你的正则表达式是寻找Python的不完全匹配的NavigableStringPython的的发生。

So your regexp is looking for an occurrence of 'Python' not the exact match to the NavigableString 'Python'.

这篇关于使用BeautifulSoup搜索字符串的html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆