Python BeautifulSoup如何从站点(corona)提取数据?

Python BeautifulSoup如何从站点(corona)提取数据?,python,web-scraping,beautifulsoup,data-extraction,Python,Web Scraping,Beautifulsoup,Data Extraction,我想以国家名称的形式保存每个国家的文章数量,我的研究工作文件中的文章数量,从以下网站。为此,我编写了这段代码,不幸的是,这段代码不起作用 结果= []您使用了错误的url。试试这个: from bs4 import BeautifulSoup # this module helps in web scrapping. import requests # this module helps us to download a web page import pandas as pd url =

我想以国家名称的形式保存每个国家的文章数量,我的研究工作文件中的文章数量,从以下网站。为此,我编写了这段代码,不幸的是,这段代码不起作用

结果=
[]

您使用了错误的url。试试这个:

from bs4 import BeautifulSoup # this module helps in web scrapping.
import requests  # this module helps us to download a web page
import pandas as pd

url = 'http://corona.sid.ir/world.svg'
data  = requests.get(url).text 
soup = BeautifulSoup(data,"lxml")  # create a soup object using the variable 'data'
soup.find_all(attrs={"class":"value"})

rows = []
for each in soup.find_all(attrs={"class":"value"}):
    row = {}
    row['country'] = each.text.split(':')[0]
    row['count'] = each.text.split(':')[1].strip()
    rows.append(row)
    
df = pd.DataFrame(rows)
输出:

print(df)
                  country count
0                 Andorra    17
1    United Arab Emirates   987
2             Afghanistan    67
3                 Albania   143
4                 Armenia    49
..                    ...   ...
179                 Yemen    54
180               Mayotte     0
181          South Africa  1938
182                Zambia   127
183              Zimbabwe   120

[184 rows x 2 columns]

这回答了你的问题吗?提交的链接中的问题和答案是一般性的,如果我的问题是次要的,并且与具有自己类型的特定站点相关。我的朋友@chitown88帮了我的忙,我发现我输入的网址不正确:)@mota,不是你输入的地址不正确,而是网站从另一个url源获取数据,然后以你原来的url呈现。2种方法是a)您可以使用原始url,但需要允许页面呈现数据,然后对其进行解析,或b)如baduker提供的链接所示,建议转到数据来源的url。我们直接去了源头。
print(df)
                  country count
0                 Andorra    17
1    United Arab Emirates   987
2             Afghanistan    67
3                 Albania   143
4                 Armenia    49
..                    ...   ...
179                 Yemen    54
180               Mayotte     0
181          South Africa  1938
182                Zambia   127
183              Zimbabwe   120

[184 rows x 2 columns]