Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.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
使用BeautifulSoup Python获取特定文本,例如“新内容”_Python_Python 2.7_Web Scraping_Beautifulsoup_Html Parsing - Fatal编程技术网

使用BeautifulSoup Python获取特定文本,例如“新内容”

使用BeautifulSoup Python获取特定文本,例如“新内容”,python,python-2.7,web-scraping,beautifulsoup,html-parsing,Python,Python 2.7,Web Scraping,Beautifulsoup,Html Parsing,我正在制作一个专注的爬虫程序,在为文档中的一个关键短语找到一个链接时遇到了一个问题 假设我想在文档中搜索的关键词是新的 将BeautifulSoup与python结合使用,我将执行以下操作 if soup.find_all(text = re.compile("Something new",re.IGNORECASE)): print true 我希望它仅在以下情况下打印为true 新事物->真实 $something new,.->真的 以下情况不适用: 这是新闻->错误 新事物

我正在制作一个专注的爬虫程序,在为文档中的一个关键短语找到一个链接时遇到了一个问题

假设我想在文档中搜索的关键词是新的

将BeautifulSoup与python结合使用,我将执行以下操作

if soup.find_all(text = re.compile("Something new",re.IGNORECASE)):
      print true
我希望它仅在以下情况下打印为true

新事物->真实

$something new,.->真的

以下情况不适用:

这是新闻->错误

新事物->错误

假设允许使用特殊字符

以前有人做过这样的事吗

谢谢你的帮助

然后,搜索新内容,不要应用re.IGNORECASE:

您也可以采用非正则表达式方法,而不是编译正则表达式模式:

for item in soup.find_all(text=lambda x: 'something new' in x):
    print item
对于上面使用的示例HTML,它还打印:

something new
$#something new,.

这是我使用的替代方法之一:

soup.find_all(text = re.compile("\\bSomething new\\b",re.IGNORECASE))

谢谢大家。

pratikgala的问题发错了吗?他只想忽略大小写和符号。但是他也想要一些新的东西->假的。这使得你的答案非常完美:-但是我必须取消对这个问题的投票:p@Md.Mohsin是的,这让我思考是否应该发布答案。代码适用于OP提供的输入,我们将看看这里是否还有smth。谢谢,谢谢你的回答。这对我很有用。find\u alltext=re.compile\\b一些新的\\b,re。IGNORECASE@pratikgala单词边界,不错的选择。谢谢分享。在这个话题上还有什么我能帮你的吗?@pratikgala很好,谢谢。你认为你应该自己回答,让别人知道吗
something new
$#something new,.
soup.find_all(text = re.compile("\\bSomething new\\b",re.IGNORECASE))