简体   繁体   English

在python中如何修复列表索引超出范围的问题?

[英]In python how can i fix list index out of range problem?

In python, I want to print each index of the list if the previous index has no value.在python中,如果前一个索引没有值,我想打印列表的每个索引。 how to print the next index how can I fix this problem I am trying web scraping and I want to get values for the website which have how can I use if loop to fix this problem?如何打印下一个索引 如何解决这个问题 我正在尝试网页抓取,我想获取网站的值,我如何使用 if 循环来解决这个问题?

this is my sample code:这是我的示例代码:

hotel_add = container.findAll("div",{"class":"is-hidden-mobile blEntry address 
ui_link_container level_4"})

hotel_add_srt= container.findAll("span",{"class":"street-address"})
Add1 = hotel_add_srt[0].text

hotel_add_ext= container.findAll("span",{"class":"extended-address"})
if
Add2 = hotel_add_ext[0].text

You can try:你可以试试:

if hotel_add_srt:
  Add1 = hotel_add_srt[0].text

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM