我正在从一个站点拉车-->https://www.aacr.org/patients-caregivers/cancer/breast-cancer/
我只想要这一页的段落信息(从开始有许多不同类型的乳腺癌..等等)
根据我的理解,一旦你在一个div和类中,进一步的循环应该“深入”并从你所在的部分/类中获取任何信息
我的代码从不同区域提取段落信息。当我运行代码时,它会提供所需的输出,但也会提供来自HTML部分的不同类/部分的段落。(<;P class='desc')它不在我声明的部分(class='first-section clearfix)中
我如何仅获取所需的输出
代码如下:
import requests
from bs4 import BeautifulSoup
import pandas as pd
PageParagraphs = []
url='https://www.aacr.org/patients-caregivers/cancer/breast-cancer/'
r=requests.get(url)
soup=BeautifulSoup(r.content,'html.parser')
section=soup.find_all('div',class_='first-section section clearfix')
for item in section:
paragraphs=soup.find_all('p')
print(paragraphs)
如果您想要包含内容的段落,可以在bs4.7.1+中使用以下内容。我使用:不排除空白和右侧方框段落。我以为你不想以源头作为结局。如果需要源参数,请删除
, :has(span)
要使用CSS选择器仅获取第一个
<p>
标记,请执行以下操作:或者使用上述代码,使用
find()
仅获取第一个<p>
标记,而不是find_all()
:编辑以获取所有段落标记:
输出:
不使用CSS选择器编辑2:
相关问题 更多 >
编程相关推荐