Python 如何在beautifulsoup中获取特定p标记后的文本?

Python 如何在beautifulsoup中获取特定p标记后的文本?,python,beautifulsoup,Python,Beautifulsoup,如何在BeautifulSoup web scraping中从此代码中获取第三个p标记后的所有文本 questions = soup.find('div',{'class':'entry-content'}) exp = questions.p[3].text (有一种像这样的方法,但我搞不懂。) 这里的任何人都可以帮忙。非常感谢这个questions=soup.find('div',{'class':'entry-content') 只找到一个p标记 你需要: question

如何在BeautifulSoup web scraping中从此代码中获取第三个p标记后的所有文本

questions = soup.find('div',{'class':'entry-content'})
exp = questions.p[3].text  

   
(有一种像这样的方法,但我搞不懂。)
这里的任何人都可以帮忙。非常感谢这个
questions=soup.find('div',{'class':'entry-content')
只找到一个
p
标记

你需要:

questions = soup.find_all('div',{'class':'entry-content'})

要查找所有
p
标记,您可以使用
[3]

尝试下面的代码,如果有帮助的话:

#This will fetch first div with class entry-content.
# In case if that is not the first div then instead use find_all and select the 
# appropriate div with help of indexing.
questions = soup.find('div', class_= 'entry-content')

#This will get all the p tags present in questions.
p_tags = questions.find_all('p')

lst=[]
for tag in p_tags[3:]:
     lst.append(tag.text)

#This will get you the text of the 4th <p> tag.
exp = p_tags[3].text
#这将获取具有类条目内容的第一个div。
#如果不是第一个div,则使用find_all并选择
#在索引的帮助下使用适当的div。
questions=soup.find('div',class='entry content')
#这将得到问题中出现的所有p标签。
p_标签=问题。查找所有('p'))
lst=[]
对于p_标签中的标签[3:]:
lst.append(tag.text)
#这将获得第四个标记的文本。
exp=p_标记[3]。文本

查看一个问题,以及如何提供感谢,但它只允许获取第三个“p”标记的文本,但我希望获取所有文本,包括第三个“p”标记后面的其他“p”标记文本。我使用
exp=p_标记[3:].text
但是我得到一个错误,AttributeError:'list'对象没有属性'text',那么您可以使用循环。遍历p_标记[3:],然后每次将文本存储到新列表中。我编辑了我的答案,将所有p标签的文本值存储到一个新列表中。