Python 基于列表理解的Web抓取

Python 基于列表理解的Web抓取,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,如果需要的话,我可以提供更多的信息和HTML块。我希望能够使用find_all命令而不仅仅是find命令将以下块转换为列表理解: soup.find(class_ = 'info-box').find_parent().find('p').text 当我尝试使用以下方法自己完成时: [p.text for p in soup.find_all(class_= 'info-box').find_parent().find('p')] [p.find_parent().find('p').tex

如果需要的话,我可以提供更多的信息和HTML块。我希望能够使用
find_all
命令而不仅仅是
find
命令将以下块转换为列表理解:

soup.find(class_ = 'info-box').find_parent().find('p').text
当我尝试使用以下方法自己完成时:

[p.text for p in soup.find_all(class_= 'info-box').find_parent().find('p')]
[p.find_parent().find('p').text for p in soup.find_all(class_= 'info-box')]
我得到以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'ResultSet' object has no attribute 'find_parent'
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
AttributeError:“ResultSet”对象没有“find\u parent”属性

您正在将
find_parent()
应用于整个
结果集,而不是单个元素。您可以尝试以下操作:

[p.text for p in soup.find_all(class_= 'info-box').find_parent().find('p')]
[p.find_parent().find('p').text for p in soup.find_all(class_= 'info-box')]

请提供一些HTML示例:是否有多个元素?明白了,我尝试了一些类似的方法,但我可以获得正确的迭代器属性顺序。这太完美了,谢谢!