【问题标题】:How extract data from the site (corona) by BeautifulSoup?BeautifulSoup 如何从站点(电晕)中提取数据?
【发布时间】:2021-05-24 08:12:59
【问题描述】:

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

http://corona.sid.ir/

!pip install bs4
from bs4 import BeautifulSoup # this module helps in web scrapping.
import requests  # this module helps us to download a web page
url='http://corona.sid.ir/'
data  = requests.get(url).text 
soup = BeautifulSoup(data,"lxml")  # create a soup object using the variable 'data'
soup.find_all(attrs={"class":"value"})

结果= []

【问题讨论】:

  • 这能回答你的问题吗? Web-scraping JavaScript page with Python
  • 提交链接中的问题和答案是一般性的,如果我的问题很小并且与具有自己类型的特定站点有关。我的朋友@chitown88 帮忙,我发现我输入了错误的网站地址:)
  • @mota,并不是您输入的地址不正确,而是该站点从其他 url 源获取数据,然后在您拥有的原始 url 中呈现。 2种方法是a)您可以使用原始url,但需要允许页面呈现数据然后解析它,或者b)如链接baduker提供的那样,建议转到数据所在的url来源于。我们只是直奔源头。

标签: python web-scraping beautifulsoup data-extraction


【解决方案1】:

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

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]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-03
    • 2022-01-11
    • 2017-11-11
    • 1970-01-01
    • 2012-08-28
    • 1970-01-01
    相关资源
    最近更新 更多