【发布时间】:2021-10-25 00:37:54
【问题描述】:
我正在尝试从三个图表中抓取数据:
网站:https://www.worldometers.info/coronavirus/country/us/ 图 1:美国冠状病毒病例总数 图 2:美国的活跃病例 图 3:美国冠状病毒总死亡人数
到目前为止,我已经从 stackoverflow 和其他位置编写/提取了代码:
from selenium import webdriver
from bs4 import BeautifulSoup as bs
import time
import json
import re
# set options to be headless, ..
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
# open it, go to a website, and get results
driver = webdriver.Chrome('chromedriver',options=options)
driver.get("https://www.worldometers.info/coronavirus/country/us/")
html = driver.page_source
soup = bs(html, "html.parser")
# get all scripts tags and select the one of interest
balises_script = soup.find_all("script")
target_balise = [str(el) for el in balises_script if "xAxis" in str(el)][0]
# use regex to extract dict like string
m = re.findall(r"xAxis = (.+)\;", target_balise)[0]
# Rest of the code
# dict like string to dict
#data = json.loads(m)
# explore data to see where data of interest is
#sub_data_of_interest = data['overview']['EngagementsSimilarweb']['WeeklyTrafficNumbers']
#for items in sub_data_of_interest.items():
# print(items)
#driver.close()
我的问题是: 我应该定位和选择的脚本标签是什么? 如何提取日期和对应的数字?
提前致谢。
【问题讨论】:
标签: python selenium beautifulsoup