简体   繁体   English

处理列表索引错误

[英]Trouble handling list index error

The script I have written to parse name, address and phone number from yellowpage is working perfectly as long as I'm excluding phone number from print statement. 只要我从打印语句中排除电话号码,我编写的用于从黄页解析名称,地址和电话号码的脚本就可以正常工作。 If I try to print the three of them, it throws error showing "list index out of range". 如果我尝试打印其中三个,则会抛出错误,显示“列表索引超出范围”。 I can't find out the remedy myself. 我自己找不到补救措施。 Here is what I have tried so far. 到目前为止,这是我尝试过的。

import requests
from bs4 import BeautifulSoup

url=requests.get("https://www.yellowpages.com/search?search_terms=Coffee%20Shops&geo_location_terms=San%20Francisco%2C%20CA&page=1")
soup=BeautifulSoup(url.text,'lxml')
for item in soup.findAll(class_="info"):
    name=item.findAll(class_="business-name")[0].text
    address=item.findAll(class_="adr")[0].text
    # phone=item.findAll(class_="phones")[0].text
    # print(name,phone,address)
    print(name,address)

Link for sir adam: " https://www.dropbox.com/s/pt9yk6y5zu9r0ag/For%20sir%20adam.txt?dl=0 " 亚当爵士的链接:“ https://www.dropbox.com/s/pt9yk6y5zu9r0ag/For%20sir%20adam.txt?dl=0

When selecting name , phone and address it's better to use find which returns only the first match instead of findAll which returns a list of all matches. 选择namephoneaddress ,最好使用find仅返回第一个匹配项的find而不是findAll返回所有匹配项的列表。

As for your question, the first item in soup.findAll(class_="info") has no 'phones' tag and so item.findAll returns an empty list, which gives you an error when you try to select the first item. 关于您的问题, soup.findAll(class_="info")的第一项没有'phones'标记,因此item.findAll返回一个空列表,当您尝试选择第一项时会出现错误。

You can handle this case either with a try - except or if - else block. 您可以使用try -except或if-else块来处理这种情况。 Examlple : 例如:

for item in soup.findAll(class_="info"):
    name=item.find(class_="business-name").text
    address=item.find(class_="adr").text
    phone=item.find(class_="phones").text if item.find(class_="phones") else None
    print(name,phone,address)

Or if you insist on using findAll : 或者,如果您坚持使用findAll

phone=item.findAll(class_="phones")[0].text if item.findAll(class_="phones") else None

Using a function : 使用功能:

def if_exist(item, item_class):
    pro=item.find(class_=item_class)
    if pro:
        return pro.text
    return ""

Example : 范例:

phone=if_exist(item, "phones")
import requests
from bs4 import BeautifulSoup

url=requests.get("https://www.yellowpages.com/search?search_terms=Coffee%20Shops&geo_location_terms=San%20Francisco%2C%20CA&page=1")
soup=BeautifulSoup(url.text,'lxml')

if __name__ == '__main__':
    for item in soup.findAll(class_="info"):
        name=item.findAll(class_="business-name")[0].text
        address = item.findAll(class_="adr")[0].text if len(item.findAll(class_="adr")) else 'No Address' 
        phone = item.findAll(class_="phones")[0].text if len(item.findAll(class_="phones")) else 'No Phones'
        print(name,phone,address)

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

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