python - 索引错误 : list index out of range in python while something not found

标签 python pandas selenium loops for-loop

如何修复 IndexError:列表索引超出范围

我正在进行数据抓取,但如果我的脚本无法找到它会给出此错误的东西

IndexError:列表索引超出范围

我想继续下一个链接不中断但我的脚本中断并且不使用第二个 url

这是我的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

plus = "+ "

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

df = pd.read_csv('Yelp+Scraping_Sans+Fransico.csv') # 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_")



    items = len(wevsite_link)
    with open("Sans Fransico.csv", 'a',encoding="utf-8") as s:
        for i in range(items):
            if wevsite_link[i].text == '':
                s.write(phone[i].text + "\n")
            if [i] == '':
                s.write('N' + "," + 'N' + "\n")
                s.write('N' + "," + 'N' + "\n")
            if wevsite_link[i].text == '' and phone[i].text == '':
                s.write('' + "," + '' + "\n")
            else:
                s.write(phone[i].text + "," + wevsite_link[i].text + "\n")

driver.close()
print ("Done")

错误:

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

最佳答案

缺失项不是空字符串,它们不存在。您可以使用 itertools.zip_longest 遍历两个列表

with open("Sans Fransico.csv", 'a',encoding="utf-8") as s:
    for combination in itertools.zip_longest(wevsite_link, phone):
        s.write(f'{combination[0].text if combination[0] else "N"}, {combination[1].text if combination[1] else "N"}\n')

关于python - 索引错误 : list index out of range in python while something not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58707118/

相关文章:

python - Pandas - 加速计算

python - 使用 Selenium 在 Flexbox 中查找元素

python - 使用 Nose/unittest 忽略测试文件

python - 如何使用 Python 向 Oracle 数据库中插入 100 万行?

python - 结束 for 循环,Python

python - 在Python中递归转置矩阵

Xpath 确定 Selenium IDE 中的复选框 "checked"属性

python - 将输出读入 os.system 命令中的变量

pandas - 如何有条件地替换pandas数据框

python - 日期时间格式混合并分开为两列并更改日期格式