【问题标题】:Python how to loop through a group of lists to get the specific indexes of the listsPython如何遍历一组列表以获取列表的具体索引
【发布时间】:2020-03-30 20:33:09
【问题描述】:

我是新手。我正在学习网络抓取,因此决定抓取一些冠状病毒数据。我想获取每个国家/地区的名称和报告的案例,它们是每个列表中的索引 0 和 1。我如何循环得到它。我读到我可以使用 Selenium 来自动化我可以在这方面提供一些帮助的数据。谢谢

import requests
import bs4 as BeautifulSoup
url = 'https://www.worldometers.info/coronavirus/'
page = requests.get(url)
page.raise_for_status()
soup = BeautifulSoup.BeautifulSoup(page.text,'html.parser')
table = soup.find('div', class_='main_table_countries_div')
data = table.find_all('tr')
row_list = list()
for tr in data:
   td = tr.find_all('td')
   row = [i.text for i in td]
   row_list.append(row)

for a in row_list:
   country_data = a
   print(country_data)








    

【问题讨论】:

标签: python selenium web-scraping


【解决方案1】:

其中一个列表是空的,当您尝试对其进行索引时会导致错误:

import requests
import bs4 as BeautifulSoup
url = 'https://www.worldometers.info/coronavirus/'
page = requests.get(url)
page.raise_for_status()
soup = BeautifulSoup.BeautifulSoup(page.text,'html.parser')
table = soup.find('div', class_='main_table_countries_div')
data = table.find_all('tr')
row_list = list()
for tr in data:
   td = tr.find_all('td')
   row = [i.text for i in td]
   row_list.append(row)

# this is erroring out because the first list is empty
print(row_list[0])
for a in row_list[1:]:
   country_data = a
   # then you can access them by index
   print(country_data[0])
   print(country_data[1])

值得注意的是,您正在重新发明轮子。如果您是为了学习而这样做的,干杯,如果不是,请查看 pandas 库来管理数据帧。

【讨论】:

    【解决方案2】:

    您非常接近,除了您所做的之外,您唯一需要做的就是提取国家名称和报告的计数。

    您的row_list 是表格中每一行的列表,因此您可以这样做:

    country = []
    reported = []
    for a in row_list:
        if len(a) > 1:
            country.append(a[0])
            reported.append(a[1])
    

    我添加了len(a) > 1 的检查,因为我认为row_list 的第一行是空的。然后countryreported 将是国家列表和每个国家按相同顺序报告的计数。

    for c, r in zip(country ,reported):
        print("{}: {}".format(c, r))
    
    
    USA: 159,689
    Italy: 101,739
    Spain: 85,195
    Germany: 66,125
    France: 44,550
    Iran: 41,495
    UK: 22,141
    Switzerland: 15,760
    Belgium: 11,899
    Netherlands: 11,750
    Turkey: 10,827
    S. Korea: 9,661
    Austria: 9,597
    Canada: 7,297
    Portugal: 6,408
    ...
    

    【讨论】:

    • 你是一个救生员。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-30
    • 2022-01-04
    • 1970-01-01
    • 2020-08-09
    相关资源
    最近更新 更多