【问题标题】:IndexError: list index out of range google-api linkedinIndexError:列表索引超出范围 google-api linkedin
【发布时间】:2018-09-13 21:35:27
【问题描述】:

*大家好, 代码如下所示(不属于我),我试图在没有环境路径的情况下运行它,这没关系,因为我以前只是用 python.exe 运行它(打开方式)。该代码用于创建包含联系方式和其他一些数据信息的 excel 文件。

但是现在当我尝试运行命令时,由于某种原因,我收到以下错误。任何帮助将不胜感激,因为我是 API 新手。 谢谢。*

我在 cmd 上运行的代码包含两个输入

打开文件后位置(cd)

lee.py "<job_role + 'email me at' + company>" 10

错误:

Traceback <most recent call last>:
File "C:\Users\z003wdaf\Desktop\linkedin\lee.py", line 15, in <module>
search_term = sys.argv[1]
IndexError: list index out of range

代码:

from googleapiclient.discovery import build                     
import datetime as dt                                           
import sys                                                      
from xlwt import Workbook                                       
import re                                                      

if __name__ == '__main__':
now_sfx = dt.datetime.now().strftime('%Y%m%d_%H%M%S')
output_dir = './output/'
output_fname = output_dir + 'srch_res_' + now_sfx + '.xls'

search_term = sys.argv[1]
num_requests = int(sys.argv[2])

my_api_key = "My API Key"                 
my_cse_id = "011658049436509675749:gkuaxghjf5u"        

service = build("customsearch", "v1", developerKey=my_api_key)

wb=Workbook()
sheet1 = wb.add_sheet(search_term[0:15])
wb.save(output_fname)
sheet1.write(0,0,'Name')
sheet1.write(0,1,'Profile Link')
sheet1.write(0,2,'Snippet')
sheet1.write(0,3,'Present Organisation')
sheet1.write(0,4,'Location')
sheet1.write(0,5,'Role')
sheet1.write(0,6,'Email')
sheet1.col(0).width = 256 * 20
sheet1.col(1).width = 256 * 50
sheet1.col(2).width = 256 * 100
sheet1.col(3).width = 256 * 20
sheet1.col(4).width = 256 * 20
sheet1.col(5).width = 256 * 50
sheet1.col(6).width = 256 * 50
wb.save(output_fname)

row = 1 


def google_search(search_term, cse_id, start_val, **kwargs):
    res = service.cse().list(q=search_term, cx=cse_id, start=start_val, **kwargs).execute()
    return res

for i in range(0, num_requests):

    start_val = 1 + (i * 10)

    results = google_search(search_term,
        my_cse_id,
        start_val,
        num=10 
    )
    for profile in range (0, 10):
        snippet = results['items'][profile]['snippet']
        myList = [item for item in snippet.split('\n')]
        newSnippet = ' '.join(myList)
        contain = re.search(r'[\w\.-]+@[\w\.-]+', newSnippet)
        if contain is not None:
            title = results['items'][profile]['title']
            link = results['items'][profile]['link']
            org = "-NA-"
            location = "-NA-"
            role = "-NA-"
            if 'person' in results['items'][profile]['pagemap']:
                if 'org' in results['items'][profile]['pagemap']['person'][0]:
                    org = results['items'][profile]['pagemap']['person'][0]['org']
                if 'location' in results['items'][profile]['pagemap']['person'][0]:
                    location = results['items'][profile]['pagemap']['person'][0]['location']
                if 'role' in results['items'][profile]['pagemap']['person'][0]:
                    role = results['items'][profile]['pagemap']['person'][0]['role']
            print(title[:-23])
            sheet1.write(row,0,title[:-23])
            sheet1.write(row,1,link)
            sheet1.write(row,2,newSnippet)
            sheet1.write(row,3,org)
            sheet1.write(row,4,location)
            sheet1.write(row,5,role)
            sheet1.write(row,6,contain[0])
            print('Wrote {} search result(s)...'.format(row))
            wb.save(output_fname)
            row = row + 1

print('Output file "{}" written.'.format(output_fname)) 

【问题讨论】:

  • 任何不完全是您的输入并且您在命令行上未受保护的&lt; 和/或&gt;cmd 解释为重定向的机会,所以像foo &lt;bar&gt; 32 这样的东西实际上变成了使用 zero 参数调用 foo,输入重定向自文件 bar,输出重定向到文件 32
  • 那么在 cmd 上输入 2 个输入(“”10)的正确格式是什么?谢谢。

标签: python api indexoutofrangeexception index-error


【解决方案1】:

当您运行文件时,您的代码需要在命令行中输入两个输入 - 第一个输入是您的搜索词,第二个输入是请求数量。

不要在终端中只运行python.exe C:\Users\z003wdaf\Desktop\linkedin\lee.py,而是添加一些额外的参数:

python.exe C:\Users\z003wdaf\Desktop\linkedin\lee.py random_thing 5

您可以将random_thing5 替换为您想要的任何内容。

【讨论】:

  • 我已经编辑并添加了我运行的包含 2 个输入的命令。我也尝试过您推荐的格式,但我收到了相同的错误消息。
猜你喜欢
  • 1970-01-01
  • 2021-11-06
  • 2011-10-31
  • 2015-06-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多