IndexError:在python中,当找不到某些内容时,列表索引超出范围

IndexError:在python中,当找不到某些内容时,列表索引超出范围,python,pandas,selenium,loops,for-loop,Python,Pandas,Selenium,Loops,For Loop,如何修复索引器:列表索引超出范围 我正在做抓取,但是如果我的脚本找不到什么东西,它会给出这个错误 索引器错误:列表索引超出范围 我想继续下一个链接不中断,但我的脚本中断,不去第二个网址 这是我的python代码: import pandas as pd from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import We

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

我正在做抓取,但是如果我的脚本找不到什么东西,它会给出这个错误

索引器错误:列表索引超出范围

我想继续下一个链接不中断,但我的脚本中断,不去第二个网址

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

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
回溯(最近一次呼叫最后一次):
文件“\seleniuminform.py”,第36行,在
s、 写入(电话[i]。文本+”,“+wevsite_链接[i]。文本+”\n”)
索引器:列表索引超出范围

如果预期会出现错误,您可以在try/except中包装主循环:

try:
   for url in mylist:
        .....

except Exception as e:
   print(e)

这将使您能够继续,并且仍然可以提供有关错误发生位置的信息。

如果错误是预期的,您可以将主循环包装为try/except´:

try:
   for url in mylist:
        .....

except Exception as e:
   print(e)

这将使您能够继续,并仍然提供有关错误发生位置的信息。

缺少的项不是空字符串,它们不存在。您可以使用
itertools.zip\u 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')

缺少的项不是空字符串,它们不存在。您可以使用
itertools.zip\u 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')


哪一行有索引器?您可以打印len(wevsite_link)这一行`如果[i]='':s.write('N'+,“+'N'+“\N”)s.write('N'+,“+'N'+“\N”)`@UsmanShahzad这是不可能的,堆栈后跟踪。好的,等等,我是posting@Guy问题更新请检查!哪一行有索引器?您可以打印len(wevsite_link)这一行`如果[i]='':s.write('N'+,“+'N'+“\N”)s.write('N'+,“+'N'+“\N”)`@UsmanShahzad这是不可能的,堆栈后跟踪。好的,等等,我是posting@Guy问题更新请检查!它关闭我的浏览器并打印错误,但这不是解决方案。我想继续下一步尝试“通过”,而不是打印异常。这会使您的浏览器保持打开状态吗?它会关闭我的浏览器并打印错误,但这不是解决方案。我想继续下一步尝试“通过”,而不是打印异常。这会让你的浏览器保持打开状态吗?@UsmanShahzad如果[I]='':,我不确定你想签入什么
,但它会创建一个带有int的列表,您正在检查此列表是否等于
。如果两者都不等于键入none,则它正在工作但不写电话例如,如果某个页面有电话号码但没有网站,则他需要写电话并跳过网站,但它正在跳过电话和网站,但它将转到下一个URL,但我还需要写一些东西,例如若网站或电话不在那个里,那个么他不需要空白,但若电话不在那个里,而网站在那个里,他需要写网站并跳过phone@UsmanShahzad如果[I]='':
,我不确定您想签入什么,但它会创建一个带有int的列表,您正在检查此列表是否等于
。如果两者都不等于键入none,则它正在工作但不写电话例如,如果某个页面有电话号码但没有网站,则他需要写电话并跳过网站,但它正在跳过电话和网站,但它将转到下一个URL,但我还需要写一些东西,例如若网站或电话不在那个里,那个么他不需要空白,但若电话不在那个里,而网站在那个里,他需要写网站并跳过电话