使用beautifulsoup4后如何分离抓取结果?

马丁·博巴克

我正在尝试从 NJR 抓取数据,http: //www.njrsurgeonhospitalprofile.org.uk/HospitalProfile?hospitalName = Abergele%20Hospital

我正处于获得正确值的地步,但是,我无法获得各自组中的值。

如果您打开上面的链接,然后单击“12 个月练习简介”下拉菜单,您可以看到不同的“操作类型”。我希望每一行都是它的特定组。

截至目前,我的代码将收集所有信息,但未将其分为特定组。事实上,在试图更多地理解我的代码时,它只是拉出在两者之间找到的所有字符串,这些字符串可能不够具体。

要么我错误地提取数据,要么我需要找出一种方法将结果分成适当的组。

import requests
from bs4 import BeautifulSoup
r=requests.get("http://www.njrsurgeonhospitalprofile.org.uk/HospitalProfile?hospitalName=Abergele%20Hospital")
c=r.content

soup=BeautifulSoup(c,"html.parser")
all=soup.find_all(["div"],{"class":"toggle_container"})[1]
print(all)

到目前为止,我能够解析出包含所有必要数据的适当 HTML 代码。接下来我遍历所有“td”对象。

i=0
for item in all.find_all("td"):
    print(all.find_all("td")[i].text)
    i=i+1
print("done")

结果:

Hip Primary
-
208
220
Hip Revision
-
Fewer Than 5
25
Knee Primary
Patello-Femoral Replacement
Fewer Than 5
4
Knee Primary
Total knee replacement
211
230
Knee Primary
Unicondylar Knee Replacement
20
26
Knee Revision
-
5
16
Shoulder Primary
-
15
16
       Total

459+
537
done

实际上,我不想要全国平均值或总行。但我可以稍后弄清楚。

克斯图利希

这可能不是您问题的最有效答案,但您可以创建一个空列表(即all_rows),其中将包含长度为 4 个项目的列表。

temp = list()
all_rows = list()
for item in all.find_all('td'):
    temp.append(item.text)
    i += 1
    if i % 4 == 0:
        all_rows.append(temp)
        temp = []

这给出了以下输出:

[
  ['Hip Primary', '-', '208', '220'],
  ['Hip Revision', '-', 'Fewer Than 5', '25'],
  ['Knee Primary', 'Patello-Femoral Replacement', 'Fewer Than 5', '4'],
  ['Knee Primary', 'Total knee replacement', '211', '230'],
  ['Knee Primary', 'Unicondylar Knee Replacement', '20', '26'],
  ['Knee Revision', '-', '5', '16'],
  ['Shoulder Primary', '-', '15', '16'],
  ['\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0Total', '', '459+', '537']
]

然后您可以将所有这些存储在字典中,其中您的键是索引 0,您的值是列表中剩余 3 个项目的列表。像这样:

all_rows_dict = dict()
for l in all_rows:
    all_rows_dict[l[0]] = l[1::]

这给出了以下输出:

{
 'Hip Primary': ['-', '208', '220'],
 'Hip Revision': ['-', 'Fewer Than 5', '25'],
 'Knee Primary': ['Unicondylar Knee Replacement', '20', '26'],
 'Knee Revision': ['-', '5', '16'],
 'Shoulder Primary': ['-', '15', '16'],
 '\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0Total': ['', '459+', '537']
}

然后要删除所有这些不间断空格,您可以创建一个像这样的干净字典:

clean_dict = {key.replace(u'\xa0', u''): value for key, value in all_rows_dict.items()}

最终输出为:

{
 'Hip Primary': ['-', '208', '220'],
 'Hip Revision': ['-', 'Fewer Than 5', '25'],
 'Knee Primary': ['Unicondylar Knee Replacement', '20', '26'],
 'Knee Revision': ['-', '5', '16'],
 'Shoulder Primary': ['-', '15', '16'],
 'Total': ['', '459+', '537']
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用BeautifulSoup4进行数据抓取的问题

如何在beautifulsoup4中根据图像内部的内容分离图像的链接

使用python和Beautifulsoup4从抓取数据中写入和保存CSV文件

如何使用python和beautifulsoup4循环抓取网站中多个页面的数据

如何使用BeautifulSoup4删除XML声明

BeautifulSoup4无法从表中抓取数据

BeautifulSoup4无法从此表中抓取数据

使用Beautifulsoup4从HTML剥离Doctype?

无法使用BeautifulSoup4抓取网站

使用BeautifulSoup抓取网站后,如何分割单词和数字?

如何在Beautifulsoup4 for Python 3.6中使用soup.find()选择结果?

如何使用BeautifulSoup4解析表并优雅地打印?

从抓取的数据中分割html(Python + BeautifulSoup4)

如何使用pd.DataFrame方法从使用beautifulsoup4抓取的信息中手动创建数据框

如何使用BeautifulSoup4使用Python修复Web抓取中的错误

无法使用BeautifulSoup4(初学者)抓取正确的Wikitable

如何使用BeautifulSoup4从客户标签中抓取信息

使用BeautifulSoup4解析网页

使用python + beautifulSoup4从动态图中抓取数据

如何安装和使用beautifulsoup4

循环不适用于使用python和beautifulsoup4抓取数据

使用BeautifulSoup4解析数据

如何使用beautifulsoup4用我的python脚本抓取更多亚马逊产品?

在python 3.6中使用beautifulsoup4抓取网站以获取产品信息时

在beautifulsoup4 中,当纯粹根据元素和其中的文本抓取网站时,如何返回多个结果?

如何使用 BeautifulSoup4 和请求获取标题的内容

如何使用 Beautifulsoup4

如何使用 python 抓取过滤后的结果(使用 selenium)?

使用 Python 抓取 HTML 中的特定元素:BeautifulSoup4