我在解析人口数量并将其附加到列表时面临困难 来自bs4进口* 导入请求
def getPopulation(name):
url="http://www.worldometers.info/world-population/"+name+"-population/"
data=requests.get(url)
soup=BeautifulSoup(data.text,"html.parser")
#print(soup.prettify())
x=soup.find_all('div',{"class":"col-md-8 country-pop-description"})
y=x[0].find_all('strong')
result=y[1].text
return result
def main():
no=input("Enter the number of countries : ")
Map=[]
for i in range(0,int(no)):
country=input("Enter country : ")
res=getPopulation(country)
Map.append(res)
print(Map)
if __name__ == "__main__":
main()
如果我通过传递一个国家名称(例如" india"作为一个参数,但是当我在这个程序中编译它时显示错误。我是python中的初学者,所以对于存在的愚蠢错误感到抱歉。
Traceback (most recent call last):
File "C:/Users/Latheesh/AppData/Local/Programs/Python/Python36/Population Graph.py", line 24, in <module>
main()
File "C:/Users/Latheesh/AppData/Local/Programs/Python/Python36/Population Graph.py", line 19, in main
res=getPopulation(country)
File "C:/Users/Latheesh/AppData/Local/Programs/Python/Python36/Population Graph.py", line 10, in getPopulation
y=x[0].find_all('strong')
IndexError: list index out of range
答案 0 :(得分:1)
我刚刚为示例案例(印度和中国)运行了代码并且没有遇到任何问题。你得到indexerror的原因是find_all没有结果,结果是[]
(所以没有第0个元素)。
要修复代码,您需要“捕获”以确认结果。这是一个基本的方法:
def getPopulation(name):
...
x=soup.find_all('div',{"class":"col-md-8 country-pop-description"})
if x:
y=x[0].find_all('strong')
result=y[1].text
else:
result = "No results founds."
return result
一种更清晰的方式来编写它,消除不必要的持有者变量(例如y)并使用三元运算符:
def getPopulation(name):
...
x=soup.find_all('div',{"class":"col-md-8 country-pop-description"})
return x[0].find_all('strong')[1].text if x else "No results founds."
关于您的代码的其他一些注意事项:
print(Map)
return Map
url
,最好使用字符串格式来插入变量。例如,"http://www.worldometers.info/world-population/{}-population/".format(name)