Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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 从标记检索内容_Python_Python 3.x_Beautifulsoup - Fatal编程技术网

Python 从标记检索内容

Python 从标记检索内容,python,python-3.x,beautifulsoup,Python,Python 3.x,Beautifulsoup,在我以前的一篇文章中,我能够检索到所有的p标签 import bs4 from urllib.request import urlopen as uReq from bs4 import BeautifulSoup as soup my_url='https://www.centralpark.com/things-to-do/central-park-zoo/polar-bears/' # opening up connection uClient = uReq(my_url) page_

在我以前的一篇文章中,我能够检索到所有的p标签

import bs4
from urllib.request import  urlopen as uReq
from bs4 import BeautifulSoup as soup

my_url='https://www.centralpark.com/things-to-do/central-park-zoo/polar-bears/'
# opening up connection
uClient = uReq(my_url)
page_html = uClient.read()
# close connection
uClient.close()
page_soup = soup(page_html, features="html.parser")

ps=list(page_soup.find_all('p'))

for s in ps:
    print(s)
我想要的是检索那些p标记中的任何内容。 例:

ex1='这是示例

'->我想要res1='这是示例' ex2='这是好的示例

'->我想要res2='这是好的示例' ex3='这是一个好例子

'->我想要res3='这是一个好例子'
所有结果(res1、res2、res3)均可进入列表


我已经搜索了解决方案,但解决方案建议只适用于一种类型的标记。我想要的只是检索p和/p之间的所有内容,而不管在这两者之间出现哪些其他标记。如果这些其他标记包含内容,则也应包含这些内容。

使用.text属性。因此,我认为,与其只是“打印”和“打印(s.text)”,还不如将每个s.text转储到结果列表中。另外,当你翻页时,找到你不需要告诉它的所有内容,然后返回一个列表。它应该已经做到了。我也尝试过处理文本,但返回异常。你能重现上面的例子吗?AttributeError:'NoneType'对象没有属性'append'
ps=page_soup.find_all('p')

results = []
for s in ps:
    #print(s.text)
    results = results.append(s.text)
ps=page_soup.find_all('p')

results = []
for s in ps:
    #print(s.text)
    results = results.append(s.text)