Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:列表迭代进行到一半时,列表索引超出范围_Python_List_Web Scraping - Fatal编程技术网

Python:列表迭代进行到一半时,列表索引超出范围

Python:列表迭代进行到一半时,列表索引超出范围,python,list,web-scraping,Python,List,Web Scraping,首先,列表索引超出范围听起来很简单,而且是不言自明的。。。但是环顾四周,我找不到对我的处境的解释;我正在遍历一个19个项目的列表,在第9个项目中,我的控制台抛出了“列表索引超出范围”错误。坦率地说,我不知道该如何解释 *我正在使用phantomjs和selenium来刮取网页…提前谢谢 # data list # -------------------------------------------------------------------------------------

首先,列表索引超出范围听起来很简单,而且是不言自明的。。。但是环顾四周,我找不到对我的处境的解释;我正在遍历一个19个项目的列表,在第9个项目中,我的控制台抛出了“列表索引超出范围”错误。坦率地说,我不知道该如何解释

*我正在使用phantomjs和selenium来刮取网页…提前谢谢

# data list
# -------------------------------------------------------------------------------------   
        xpath = [
        businessName,firstName,lastName,ubi,info,
        licenseType,licenseNumber,licenseEffectiveDate,licenseExpirationDate,status,
        bondProvider,bondNumber,bondAmount,bondEffectiveDate,bondEffectiveDate,insuranceProvider,
        insuranceNumber,insuranceAmount,insuranceEffectiveDate,insuranceExpirationDate
        ] 

        data = [
        "businessName","firstName","lastName","ubi","info",
        "licenseType","licenseNumber","licenseEffectiveDate","licenseExpirationDate","status",
        "bondProvider","bondNumber","bondAmount","bondEffectiveDate","bondEffectiveDate","insuranceProvider",
        "insuranceNumber","insuranceAmount","insuranceEffectiveDate","insuranceExpirationDate"
        ] 
#
#
# xpath check and grab function
# -------------------------------------------------------------------------------------
        i = 0
        while i <= len(data):
           result = browser.find_element_by_xpath(xpath[i]).text    #checks is xpath exists
           print i
           print data[i] + " = " + str(result) 
           i += 1
#数据列表
# -------------------------------------------------------------------------------------   
xpath=[
企业名称、姓氏、姓氏、ubi、信息、,
licenseType、licenseNumber、licenseEffectiveDate、licenseExpirationDate、状态、,
债券提供方、债券编号、债券金额、债券生效日期、债券生效日期、保险提供方、,
保险编号、保险金额、保险生效日期、保险到期日期
] 
数据=[
“businessName”、“firstName”、“lastName”、“ubi”、“info”,
“licenseType”、“licenseNumber”、“licenseEffectiveDate”、“licenseExpirationDate”、“status”,
“债券提供方”、“债券编号”、“债券金额”、“债券生效日期”、“债券生效日期”、“保险提供方”,
“保险编号”、“保险金额”、“保险生效日期”、“保险到期日期”
] 
#
#
#xpath检查和抓取函数
# -------------------------------------------------------------------------------------
i=0

虽然i如您的示例所示,如果
xpath
data
中的项目始终直接对应,您可以通过使用循环表达式更轻松、更清晰地执行此操作:

for elem_xpath, name in zip(xpath, data):
如果您还需要索引
i
,请使用
enumerate

for i, (elem_xpath, name) in enumerate(zip(xpath, data)):

什么是
i您的while循环肯定会超出范围,您确定错误发生在第9次迭代时吗?请尝试更改while(i)您最好使用
作为范围内的i(len(xpath)-1
而不是一个while循环。我也被
I@JacobH搞糊涂了,我已经更新了你建议的内容,在第9次迭代中仍然得到了一个erron。这非常有用,我会使用它……但我刚刚发现,注释掉下面的行可以让我通过第9次迭代:“result=browser.find_element_by_xpath”(xpath[i]).text“