初学爬虫,请问为什么获取数据是运行报错,该如何解决?
代码如下
# 获取网页中包含数据链接的列表信息
# 1
import requests
import re
import json
from bs4 import BeautifulSoup
def getOriHtmlText(url,code='utf-8'):
try:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
}
r=requests.get(url,timeout=30,headers=headers)
r.raise_for_status()
r.encoding=code
return r.text
except:
return "There are some errors when get the original html!"
def getTheList(url):
html=getOriHtmlText(url)
soup=BeautifulSoup(html,'html.parser')
# script=soup.find_all('script',{"id":"getListByCountryTypeService2true"})
# print(script.find(''))
htmlBodyText=soup.body.text
# 获取国家数据
worldDataText=htmlBodyText[htmlBodyText.find('window.getListByCountryTypeService2true = '):]
worldDataStr = worldDataText[worldDataText.find('[{'):worldDataText.find('}catch')]
worldDataJson=json.loads(worldDataStr)
with open("../data/worldData.json","w") as f:
json.dump(worldDataJson,f)
print("写入国家数据文件成功!")
# 获取各省份数据
provinceDataText = htmlBodyText[htmlBodyText.find('window.getAreaStat = '):]
provinceDataStr = provinceDataText[provinceDataText.find('[{'):provinceDataText.find('}catch')]
provinceDataJson=json.loads(provinceDataStr)
with open("../data/provinceData.json","w") as f:
json.dump(provinceDataJson,f)
print("写入省份数据文件成功!")
getTheList("https://ncov.dxy.cn/ncovh5/view/pneumonia")
报错内容
Traceback (most recent call last):
File "E:/桌面/getdata.py", line 39, in <module>
getTheList("https://ncov.dxy.cn/ncovh5/view/pneumonia")
File "E:/桌面/getdata.py", line 27, in getTheList
worldDataJson=json.loads(worldDataStr)
File "C:\Users\13076\AppData\Local\Programs\Python\Python38\lib\json\__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "C:\Users\13076\AppData\Local\Programs\Python\Python38\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\13076\AppData\Local\Programs\Python\Python38\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)