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

标签 python web-scraping beautifulsoup python-requests

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

关于python - 当html中没有显示数字时,如何从网站中提取表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53048057/

相关文章:

python - 这里可以理解吗?

javascript - Casperjs 抓取动态内容

python - 点不可点击 - SELENIUM - PYTHON

python - 使用 python 向 aspx 页面提交查询并抓取结果?

python - 在 Beautifulsoup 的 find_all 中使用正则表达式

python - 如何使用 python 进行 "google" "bing" "yahoo"搜索?

python - 基于列名在 pandas 数据框的 lambda 表达式上使用 if else 语句

python - 如何将多个网站页面的抓取结果保存到 CSV 文件中?

python - 删除另一个标签内的标签 beautifulsoup

python - Soup 没有从 div 中找到特定的类