如何修复 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/