IndexError:列表索引超出了Python中循环的范围

您好,我想报废的Everone,但是您在59岁时遇到了这个错误

我的xlsx文件中有1089个项目

错误:

Traceback (most recent call last):
  File ".\seleniuminform.py",line 28,in <module>
    s.write(phone[i].text + "," + wevsite_link[i].text + "\n")
IndexError: list index out of range

这是我的python代码:

import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException


with open("Sans Fransico.csv","r") as s:
    s.read()

df = pd.read_excel('myfile.xlsx') # Get all the urls from the excel
mylist = df['Urls'].tolist() #urls is the column name

driver = webdriver.Chrome()

for url in mylist:

    driver.get(url)
    wevsite_link = driver.find_elements_by_css_selector(".text--offscreen__373c0__1SeFX+ .link-size--default__373c0__1skgq")

    phone = driver.find_elements_by_css_selector(".text--offscreen__373c0__1SeFX+ .text-align--left__373c0__2pnx_")

    num_page_items = len(phone)
    with open("Sans Fransico.csv",'a',encoding="utf-8") as s:

        for i in range(num_page_items):
            s.write(phone[i].text + "," + wevsite_link[i].text + "\n")

driver.close()
print ("Done")

链接:

https://www.yelp.com/biz/daeho-kalbijjim-and-beef-soup-san-francisco-9?osq=Restaurants

网站和电话中的错误:

IndexError:列表索引超出了Python中循环的范围

oliver112233 回答:IndexError:列表索引超出了Python中循环的范围

我对Selenium不太熟悉,因此我无法对此发表评论。

第一次打开“ Sans Francisco.csv”时,您将读取内容而不将其分配给变量。

对于您的错误,这是由于您的范围基于phone的长度,而不是wevsite_link的长度而引起的。如果wevsite_link短于phone,则会出现错误。简而言之,您发现网站链接少于电话号码,但是您的代码假定您将始终找到完全相同的数量。

您能再解释一下您的代码吗?你想做什么?

,

我一眼就怀疑

 @Override
    public Cursor query(@NonNull Uri uri,@Nullable String[] projection,@Nullable String selection,@Nullable String[] selectionArgs,@Nullable String sortOrder) {
        int match=urim.match(uri);

        if(helper == null){
           helper = new DatatBaseHelper(context) // initialize your helper.
        }
        db=helper.getReadableDatabase();
        // ........
}

返回0。也许您要查找匹配项的css选择器不正确。

,

似乎有些物品没有手机,因此发现的手机数量少于网站。

您宁愿先找到所有".text--offscreen__373c0__1SeFX+",然后再使用for循环在每个项目中分别搜索phonewebsite

使用try/except可以识别项目是否没有电话,并使用空字符串作为电话号码

for url in mylist:

    driver.get(url)

    all_items = driver.find_elements_by_css_selector(".text--offscreen__373c0__1SeFX+")

    for item in all_items:
        try:
            wevsite_link = item.find_element_by_css_selector(".link-size--default__373c0__1skgq")
            wevsite_link = wevsite_link.text
        #except selenium.common.exceptions.NoSuchElementException:
        except:
            wevsite_link = ''

        try:
            phone = item.find_element_by_css_selector(".text-align--left__373c0__2pnx_")
            phone = phone.text
        #except selenium.common.exceptions.NoSuchElementException:
        except:
            phone = ''

        with open("Sans Fransico.csv",'a',encoding="utf-8") as s:
             s.write(phone + "," + wevsite_link + "\n")

我没有指向该页面的网址,所以无法对其进行测试。

本文链接:https://www.f2er.com/3162177.html

大家都在问