如何将包含一个元素的列表转换为包含 python 中的多个元素的列表
How do I convert a list with one element into a list with many elements in python
我正在尝试创建一个元素中的列表。因此我需要将一个元素转换为 may: containing the dates.
代码:
import requests
from bs4 import BeautifulSoup
import ast
#gets the dates
URL = ("https://www.worldometers.info/coronavirus/country/us/")
page = requests.get(URL)
soup = BeautifulSoup(page.content, "html.parser")
results=soup.find_all("div", {"class": "col-md-12"})
data=results[4]
script = data.find('script')
string = script.string
datesTEMP = string.strip()[292:]#-9268]
x=datesTEMP
i=0
xaxis=[]
count=0
while count<1:
if x[i]=="]":
count=count+1
else:
xaxis.append(x[i])
i=i+1
xaxislength=len(xaxis)
xaxis = [''.join(xaxis[0:xaxislength])]
print(xaxis)
您尝试从 categories: [
和 ]
之间的 JavaScript 数据 (Highcharts
) 中获取值 - 因此您可以使用 split()
获取此值部分。如果您稍后添加 [
和 ]
- "[" + string + "]"
- 那么您将得到带有 JSON 数据的字符串,您可以使用模块 json
[ 将其转换为列表=23=]
这给出了包含 535 个元素的列表
import requests
from bs4 import BeautifulSoup
import json
url = "https://www.worldometers.info/coronavirus/country/us/"
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
results = soup.find_all("div", {"class": "col-md-12"})
string = results[4].find('script').string
# remove string before dates
substring = string.split("categories: [")[1]
# remove string after dates
substring = substring.split("]")[0]
# create back string with list
substring = "[" + substring + "]"
#print(substring)
# convert string with JSON data into Python list
dates = json.loads(substring)
print('len(dates):', len(dates))
顺便说一句:
差不多 1.5 年前我对同一页的回答:
Web scrape coronavirus interactive plots
我在我的博客上也找到了代码:
Scraping: How to get data from interactive plot created with HighCharts
但我宁愿尝试从 GitHub CSSEGISandData / 获取 CSV 文件
COVID-19 并将其加载到 pandas
并计算 pandas
中的所有值。我想我很久以前在一些回答中描述过它。
我正在尝试创建一个元素中的列表。因此我需要将一个元素转换为 may: containing the dates.
代码:
import requests
from bs4 import BeautifulSoup
import ast
#gets the dates
URL = ("https://www.worldometers.info/coronavirus/country/us/")
page = requests.get(URL)
soup = BeautifulSoup(page.content, "html.parser")
results=soup.find_all("div", {"class": "col-md-12"})
data=results[4]
script = data.find('script')
string = script.string
datesTEMP = string.strip()[292:]#-9268]
x=datesTEMP
i=0
xaxis=[]
count=0
while count<1:
if x[i]=="]":
count=count+1
else:
xaxis.append(x[i])
i=i+1
xaxislength=len(xaxis)
xaxis = [''.join(xaxis[0:xaxislength])]
print(xaxis)
您尝试从 categories: [
和 ]
之间的 JavaScript 数据 (Highcharts
) 中获取值 - 因此您可以使用 split()
获取此值部分。如果您稍后添加 [
和 ]
- "[" + string + "]"
- 那么您将得到带有 JSON 数据的字符串,您可以使用模块 json
[ 将其转换为列表=23=]
这给出了包含 535 个元素的列表
import requests
from bs4 import BeautifulSoup
import json
url = "https://www.worldometers.info/coronavirus/country/us/"
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
results = soup.find_all("div", {"class": "col-md-12"})
string = results[4].find('script').string
# remove string before dates
substring = string.split("categories: [")[1]
# remove string after dates
substring = substring.split("]")[0]
# create back string with list
substring = "[" + substring + "]"
#print(substring)
# convert string with JSON data into Python list
dates = json.loads(substring)
print('len(dates):', len(dates))
顺便说一句:
差不多 1.5 年前我对同一页的回答:
Web scrape coronavirus interactive plots
我在我的博客上也找到了代码:
Scraping: How to get data from interactive plot created with HighCharts
但我宁愿尝试从 GitHub CSSEGISandData / 获取 CSV 文件
COVID-19 并将其加载到 pandas
并计算 pandas
中的所有值。我想我很久以前在一些回答中描述过它。