Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.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?_Python_Google App Engine_Beautifulsoup - Fatal编程技术网

Python 我怎样才能模仿“我的生活”包括「;使用BeautifulSoup?

Python 我怎样才能模仿“我的生活”包括「;使用BeautifulSoup?,python,google-app-engine,beautifulsoup,Python,Google App Engine,Beautifulsoup,我正在做一个项目,我需要做一些修改。该项目在谷歌应用程序引擎上,我们目前正在使用Python 2.5。理想情况下,我们会使用,但由于在AppEngine和Python2.5上运行,所以这不是一个选项 我见过像这样的问题,但它们不太中肯 我有一些HTML如下所示: <div class="post"> <div class="description"> This post is about <a href="http://www.wikiped

我正在做一个项目,我需要做一些修改。该项目在谷歌应用程序引擎上,我们目前正在使用Python 2.5。理想情况下,我们会使用,但由于在AppEngine和Python2.5上运行,所以这不是一个选项

我见过像这样的问题,但它们不太中肯

我有一些HTML如下所示:

<div class="post">
    <div class="description">
        This post is about <a href="http://www.wikipedia.org">Wikipedia.org</a>
    </div>
</div>
<!-- More posts of similar format -->
我天真地认为我可以在BeautifulSoup做这样的事情:

soup = BeautifulSoup(html)
soup.findAll(True, "post", text=("This post is about Google.com"))
# []
然而,这没有产生任何结果。我将我的查询更改为使用正则表达式,并得到了进一步的改进,但仍然不走运:

soup.findAll(True, "post", text=re.compile(".*This post is about.*Google.com.*"))
# []
如果我省略了
Google.com
,它会起作用,但是我需要手动执行所有过滤是否仍然可以使用BeautifulSoup模拟
:包含


或者,是否有一些类似PyQuery的库可以在App Engine(Python 2.5)上运行?

来自BeautifulSoup文档(我的重点):

“文本是一个参数,用于搜索NavigableString对象 而不是标签“

也就是说,您的代码:

soup.findAll(True, "post", text=re.compile(".*This post is about.*Google.com.*"))
不同于:

regex = re.compile('.*This post is about.*Google.com.*')
[post for post in soup.findAll(True, 'post') if regex.match(post.text)]
您必须删除Google.com的原因是,在
的BeautifulSoup树中有一个NavigableString对象。这篇文章是关于“
”,另一篇是关于
“Google.com”
,但它们位于不同的元素下


顺便说一句,
post.text
是存在的,但没有文档记录,所以我也不会依赖它,我是偶然编写了那段代码的!使用其他方法将
post

下的所有文本混在一起,为什么不迁移到2.7 where?我们当然想,只是还没能。旧的代码库,没有足够的时间,等等。这是一个公平的批评。好吧,问题似乎不是太复杂,因为你的应用程序是版本化的,你可以尝试一下,如果它不起作用,就向后移动。谢谢你的建议。我们确实试过了。我们过去的一位开发人员太聪明了,这让事情变得相当复杂:(我确实读过那句话,但无法理解其中的区别。这正是我所需要的。谢谢:)@NT3RP“无法理解其中的区别”:对我来说,同样,BeautifulSoup文档真的是一团糟。;)
regex = re.compile('.*This post is about.*Google.com.*')
[post for post in soup.findAll(True, 'post') if regex.match(post.text)]