首页 > 解决方案 > 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

标签: pythonweb-scrapingbeautifulsouppython-requests

解决方案


您可以使用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

推荐阅读