Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/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
Python 3.x bs4使用find_all后如何使用find?_Python 3.x_Beautifulsoup - Fatal编程技术网

Python 3.x bs4使用find_all后如何使用find?

Python 3.x bs4使用find_all后如何使用find?,python-3.x,beautifulsoup,Python 3.x,Beautifulsoup,我想搜索一个bbs,并找到一个特定用户的内容。以下是我的代码: import requests from bs4 import BeautifulSoup main_html = requests.get('http://bbs.tianya.cn/post-free-2328897-1.shtml') main_soup = BeautifulSoup(main_html.text, 'lxml') all_a = main_soup.find_all('div', _host='羡鱼则结

我想搜索一个bbs,并找到一个特定用户的内容。以下是我的代码:

import requests
from bs4 import BeautifulSoup


main_html = requests.get('http://bbs.tianya.cn/post-free-2328897-1.shtml')
main_soup = BeautifulSoup(main_html.text, 'lxml')
all_a = main_soup.find_all('div', _host='羡鱼则结网')
现在我有了这个用户的所有代码,我想再次搜索内容。但是,我不能使用find,因为find\u all返回一个列表。
要通过BeautifulSoup搜索两次,我应该怎么做?

是否要在列表中的每个项目中搜索?然后只需迭代它:
for a in all_a:…
。我最近遇到了一个类似的问题,我使用了2个for循环:for div in all:for div in divs:每个项目都由div表示,您现在可以使用所需的属性。如果这有帮助,这就是我用于解决问题的代码片段。。。请注意,如果不调整:
soup=BeautifulSoup(source,'html.parser')content=soup.findChildren(class_='content-section')for ptag.findAll('p'):print(i.text.encode('utf-8'))