爬取疫情数据

import numpy as np
from pandas import Series, DataFrame
import os
import requests

from bs4 import BeautifulSoup


def get_urls(url):
    response = requests.get(url).text
    bs = BeautifulSoup(response, "html.parser")
    hrefs = bs.find("table", id="main_table_countries_today").find_all("tbody")[0].find_all("tr", attrs={
   "class": ""})
    urls = []
    labels = []
    for i, href in enumerate(hrefs):

        try:
            urls.append(href.td.a.get("href"))
            labels.append(href.td.a.text.replace(" ", ""))
        except Exception as e:
            print(e)
    return urls, labels


def get_data(url, label):
    print(label)
    response = requests.get(url).text
    bs = BeautifulSoup(response, "html.parser")
    scs = bs.select("script")
    data_1 = []

    if label in np.array(
            ["UK", "Turkey", "Russia", "Netherlands", "Peru", "Portugal", "Ecuador", "Ireland", "Singapore", "UAE",
             "Qatar", "Indonesia", "Romania", "S.Korea", "Belarus", "Serbia", "Philippines", "Nicaragua", "Switzerland",
             "Czechia", "DominicanRepublic", "Malaysia", "Norway", "Ukraine", "Bangladesh", "Colombia", "SouthAfrica",
             "Egypt", "Morocco", "Moldova", "Algeria", "Kuwait", "Kazakhstan", "Bahrain", "Greece", "Hungary",
             "Croatia", "Oman", "Uzbekistan", "Iraq", "Armenia", "Azerbaijan", "Cameroon", "Ghana",
             "Afghanistan""BosniaandHerzegovina", "NewZealand", "Lithuania", "Slovenia", "NorthMacedonia", "Slovakia",
             "Afghanistan", "BosniaandHerzegovina", "Cuba", "Bulgaria", "Nigeria", "IvoryCoast", "HongKong", "Djibouti",
             "Guinea", "Tunisia", "Bolivia", " Cyprus", "Latvia", "Andorra", "Albania", "Lebanon", "CostaRica", "Niger",
             "Kyrgyzstan", "Estonia", "Cyprus", "Andorra", "Senegal", "BurkinaFaso", "Honduras", "Uruguay", "SanMarino",
             "ChannelIslands", "SriLanka", "Georgia", "Guatemala", "Malta", "Jordan", "DRC", "Somalia", "Taiwan",
             "Mayotte", "Mali", "Kenya", "Palestine", "Mauritius", "Venezuela", " Montenegro", "IsleofMan", "Bahamas",
             "Barbados"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值