python - html中不显示数字时如何从网站中提取表格?
问题描述
我正在尝试在下面的网站中提取所有行业和时期的表格。但是,当我下载 html 时,在任何地方都找不到网站中显示的数字。如何检索表的条目?
https://csimarket.com/Industry/industry_Efficiency.php?ind=102
我下面的代码提取了 html。经检查,表格中的数字没有出现在任何地方,所以我无法提取它们。那么问题来了,他们在哪里?以及如何访问和提取它们?
请注意,我是新来的请求和美丽的汤!非常感谢!
import requests
my_target='https://csimarket.com/Industry/industry_Efficiency.php?ind=102'
r = requests.get(my_target)
data = r.text
解决方案
您可以使用requests
,但您需要使用,r.content
而不是r.text
:
import requests
my_target='https://csimarket.com/Industry/industry_Efficiency.php?ind=102'
r = requests.get(my_target)
data = r.content
您也可以使用BeautifulSoup
模块来解析html
,如下所示:
import requests
#load beautifullsoup
from bs4 import BeautifulSoup
my_target='https://csimarket.com/Industry/industry_Efficiency.php?ind=102'
r = requests.get(my_target)
#get raw html
data = r.content
#soup the content
soup = BeautifulSoup(data, 'html.parser')
#find table element
table_element = soup.find('table',{"class":"osnovna_tablica_bez_gifa"})
#print text version of table element
print table_element.text
哪个给你:
Chemicals - Plastics & Rubber Industry
3 Q
2 Q
1 Q
4 Q
3 Q
2018
2018
2018
2017
2017
Revenue/Employee (TTM) $
428,075
327,852
323,322
338,175
325,069
Sales/Employee Ranking
# 22
# 78
# 79
# 68
# 74
Net Income/Employee (TTM) $
37,510
18,571
20,953
27,151
18,810
Net Income/Employee
Ranking
# 16
# 72
# 69
# 58
# 64
Receivable Turnover Ratio (TTM)
7.53
5.17
5.07
5.17
5.11
Receivable Turnover Ranking
# 31
# 88
# 90
# 87
# 89
Inventory Turnover Ratio (TTM) Sales
8.1
5.56
5.65
6.13
6.45
Inventory Turnover (Sales)
Ranking
# 31
# 90
# 90
# 86
# 85
Inventory Turnover Ratio (TTM) COS
5.77
3.83
3.81
4.16
4.37
Inventory Turnover (COS)
Ranking
# 24
# 79
# 81
# 75
# 77
Asset Turnover Ratio (TTM)
0.92
0.47
0.52
0.6
0.69
Asset Turnover Ranking
# 31
# 72
# 68
# 63
# 49
推荐阅读
- swift - NSAttributedString 中的块引用
- c - 为什么 C 程序中的 GPIO 权限与控制台不同?
- javascript - WebGL 未正确渲染 3d 对象
- sql - 使用额外的列将记录从一个 Oracle SQL 表复制到另一个
- python - 如何让 Instagram 关注者列表向下滚动并继续关注?
- javascript - THREE.JS - 两张脸的交点处的毛刺线
- python - 将前导零添加到日期时间列的分钟部分
- javascript - 为什么 Typescript 抱怨一个对象不能分配给它的类型?
- sql - 循环假脱机脚本
- javascript - 如何使用 JavaScript 从表数据计算正常运行时间