当数字没有在html中显示时,如何从网站中提取表格?

2024-09-26 22:08:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图提取下表中的所有行业和时期的网站。然而,当我下载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

Tags: httpscomtarget网站myhtml地方数字
3条回答

使用熊猫会更整洁read_html

import pandas as pd
tables = pd.read_html('https://csimarket.com/Industry/industry_Efficiency.php?ind=102')
print(tables[6].fillna(''))

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

您也可以使用^{} module来解析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

您可以使用urllib包,然后使用正则表达式提取数字。执行:

import urllib3
from bs4 import BeautifulSoup

http = urllib3.PoolManager()

url = "https://csimarket.com/Industry/industry_Efficiency.php?ind=102"
response = http.request('GET', url)

soup = BeautifulSoup(response.data)

spans = soup.find_all("span")

for span in spans:

  print(span)

这将提供:

<span class="">428,075</span>
<span class="">327,852</span>
<span class="">323,322</span>
...
<span class="siva3">31</span>
<span class="siva3"># 5</span>
<span class="siva3"># 31</span>

相关问题 更多 >

    热门问题