我的代码在下面给出
import requests
import re
from bs4 import BeautifulSoup
page = requests.get(
"https://catalog.data.gov/dataset?q=&sort=metadata_created+desc")
soup = BeautifulSoup(page.content, 'html.parser')
# value = soup.find_all(class_='new-results')
for hit in soup.findAll(attrs={'class': 'dataset-heading'}):
print(hit.text)
我的结果在几行,例如
涵洞
爱荷华州地理地图服务器
在大气模型中,基于涡度的参数化,用于对流层/下层臭氧的指定
证明了从体外雌激素受体转录激活测定法(T47D-kbluc)到体内子营养测定法的体外雌激素受体转录激活测定法(T47D-kbluc)的不确定性的不确定性。
MRPAT模拟的数据
水线ATS BG消毒数据
工业无线测量分析和场景生成的计算机代码
我的问题:
如何仅获得第一行,例如。在这种情况下,"涵洞"
或如何从BS4 Findall结果获取第一行?
我在您的代码中修改了一点。
import requests
import re
from bs4 import BeautifulSoup
page = requests.get(
"https://catalog.data.gov/dataset?q=&sort=metadata_created+desc")
soup = BeautifulSoup(page.content, 'html.parser')
# value = soup.find_all(class_='new-results')
#for hit in soup.find(attrs={'class': 'dataset-heading'}).text:
a = soup.find(attrs={'class': 'dataset-heading'}).text
print a
正如@SID所说,使用查找仅获取第一个元素。无需用于循环和Findall。
尝试soup.find
而不是soup.findAll
。
这只会返回第一个结果。