最近参加了江苏省的数模省赛,做了一个题目,是关于疫情的,我们选择腾讯的疫情数据作为爬取对象,相关的代码如下。为了测试爬虫历时,我还记录了打印操作。
import openpyxl
import requests
import time
start = time.time()
urlList = [
"https://api.inews.qq.com/newsqa/v1/automation/foreign/daily/list?country=%E7%BE%8E%E5%9B%BD&",
"https://api.inews.qq.com/newsqa/v1/automation/foreign/daily/list?country=%E6%84%8F%E5%A4%A7%E5%88%A9&",
"https://api.inews.qq.com/newsqa/v1/automation/foreign/daily/list?country=%E6%B3%95%E5%9B%BD&",
"https://api.inews.qq.com/newsqa/v1/automation/foreign/daily/list?country=%E6%BE%B3%E5%A4%A7%E5%88%A9%E4%BA%9A&",
"https://api.inews.qq.com/newsqa/v1/automation/foreign/daily/list?country=%E9%9F%A9%E5%9B%BD&",
"https://api.inews.qq.com/newsqa/v1/automation/foreign/daily/list?country=%E5%8D%B0%E5%BA%A6&"
]
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/83.0.4103.61 Safari/537.36 "
}
countryNameList = ["American",
"Italy",
"France",
"Australia",
"Korea",
"India"
]
def dataSavedFunction(url):
response = requests.get(url, headers=headers)
jsonResponse = response.json()
dataCollection = []
for result in jsonResponse["data"]:
dataCollection.append([
result["date"],
result["confirm"],
result["dead"],
result["heal"],
result["confirm_add"]
])
return dataCollection
try:
def dataToExcel():
wb = openpyxl.Workbook()
for name in countryNameList:
wb_sheet = wb.create_sheet(name)
wb_sheet.append(["日期", "累积确诊", "累积死亡", "累计治愈", "现有新增确诊"])
nameIndex = countryNameList.index(name)
rows = dataSavedFunction(urlList[nameIndex])
for j in rows:
wb_sheet.append(j)
wb.save("totalCrawlResult.xlsx")
wb.close()
except PermissionError:
print("文件读写错误!该文件已经被打开,请关掉文件再试")
if __name__ == "__main__":
dataToExcel()
end = time.time()
print("本次爬虫历时:", end - start, "秒")
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78