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

Python 当数字没有显示在html中时,如何从网站中提取表?,python,web-scraping,beautifulsoup,python-requests,Python,Web Scraping,Beautifulsoup,Python Requests,我正在尝试提取以下网站中所有行业和时期的表格。然而,当我下载html时,网站上显示的数字在任何地方都找不到。如何检索表的条目 下面的代码提取html。经检查,表中的数字没有出现在任何地方,因此我无法提取它们。问题是他们在哪里?如何访问和提取它们 请注意,我是新来的要求和美丽的汤! 非常感谢 import requests my_target='https://csimarket.com/Industry/industry_Efficiency.php?ind=102' r = reque

我正在尝试提取以下网站中所有行业和时期的表格。然而,当我下载html时,网站上显示的数字在任何地方都找不到。如何检索表的条目

下面的代码提取html。经检查,表中的数字没有出现在任何地方,因此我无法提取它们。问题是他们在哪里?如何访问和提取它们

请注意,我是新来的要求和美丽的汤! 非常感谢

import requests

my_target='https://csimarket.com/Industry/industry_Efficiency.php?ind=102'

r  = requests.get(my_target)
data = r.text

您可以使用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>
428075
327,852
323,322
...
31
# 5
# 31

您可以使用
请求
,但您需要使用
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
您还可以使用解析
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

使用pandas
read\uhtml

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

请求不会加载Javascript生成的数据,因此它不会显示在beautifulsoup@cricket_007,有什么解决方法吗?Selenium Webdriver是更常用的替代方法。非常感谢,现在可以找到这些数字了!是否有一种更好的方法来提取您正在考虑的数据?您的数据存储在span元素中,因此您可以使用BeautifulSoup在那里提取数据,然后对其进行处理。也可以直接使用find_all('tr')或find_all('td')查找所有“tr”或“td”元素,或者如果您只需要一个特定的元素,则可以使用与查找表元素相同语法的类或id来查找所有“tr”或“td”元素