pandas 从网站获取数据-使用BS 4和请求

pw136qt2  于 2023-05-27  发布在  其他
关注(0)|答案(1)|浏览(123)

我试图从网站https://www.startupblink.com刮数据与美丽的汤,Pyhon和请求

from bs4 import BeautifulSoup
import requests

url = "https://www.startupblink.com"
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")

links = soup.find_all("a")

for link in links:
    print(link.get("href"))

这将找到页面上的所有标记并打印出它们的href属性的值。我的数据提取要求如下:我想把所有的数据从网站
顺便说一句,如果是Pandas,那就更容易了。!
使用pandas库可以使抓取和处理数据的过程更加容易。Pandas提供了强大的数据操作和分析工具,包括直接从URL阅读HTML表的方便函数。以下是我关于如何使用pandas从网站https://www.startupblink.com中抓取数据的一些想法:

import pandas as pd
    import requests
    Send a GET request to the website: Send a GET request to the URL we want to scrape and store the response in a variable:
    
    url = "https://www.startupblink.com"
    response = requests.get(url)

首先,我们使用pandas读取HTML表:这里我们使用pandas的read_html()函数来解析HTML并提取页面上的表。此函数返回表示找到的表的DataFrame对象的列表。在本例中,由于我们对整个页面上的表感兴趣,因此可以将response.content传递给read_html():
tables = pd.read_html(response.content)处理和使用数据:一旦我们有了表示表的DataFrame对象,我们就可以使用pandas的内置函数和方法来处理和分析数据。
你对这些不同的方法有什么看法?

bttbmeg0

bttbmeg01#

不知道你是什么信息后,但也许API将为您工作?

import json

import pandas as pd
import requests

def get_data() -> pd.DataFrame:
    url = "https://www.startupblink.com/api/leaderboards?leaderboard_type=Cities&industry=leaderboard&year=2022"

    with requests.Session() as request:
        response = request.get(url, timeout=10)
    if response.status_code != 200:
        print(response.raise_for_status())

    data = json.loads(response.text)

    return pd.DataFrame(data=data)

print(get_data())

输出(几行):

global_rank prev_global_rank  national_rank prev_national_rank  population  country_id            country_name  city_id                                display_name quantity_score quality_score business_score quality_factor1  quality_factor change_national change_global change
0              1                1              1                  1   9666055.0           1           United States        5            San Francisco Bay, United States         36.186       510.423          3.660        550.2690         550.269               0             0    new
1              2                2              2                  2  21045000.0           1           United States       15                     New York, United States         18.339       195.003          3.660        217.0020         217.002               0             0    new
2              3                5              1                  1   9176530.0           5          United Kingdom       11                      London, United Kingdom         21.673       100.171          3.793        125.6370         125.637               0             2    new
3              4                4              3                  3   3971883.0           1           United States       21             Los Angeles Area, United States         14.677        95.518          3.660        113.8550         113.855               0             0    new
4              5                6              4                  4   4771936.0           1           United States       63                  Boston Area, United States          8.663        95.727          3.660        108.0500          108.05               0             1    new
5              6                3              1                  1  20383994.0          45                   China      171                              Beijing, China          7.112        92.931          2.652        102.6950         102.695               0            -3    new
6              7                7              2                  2  22315474.0          45                   China      293                             Shanghai, China          5.097        62.868          2.652         70.6165         70.6165               0             0    new

相关问题