我有一个人和公司的列表,正在尝试搜索Google以查找这些人的linkedin URL。
我的代码:
#!/usr/bin/python3
import json
import urllib2
import time
file = open('names_and_companies.txt', 'r')
lines =file.readlines()
file.close()
file_out = open('out-urls.txt', 'w')
for line in lines:
line = line.strip()
lst = line.split(",")
search_term = lst[0] + " " + lst[1] #construct a search term using name and company
search_term = search_term.replace(" ","+")
time.sleep(1)
encoded = urllib2.quote(search_term)
rawData = urllib2.urlopen ('http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q='+encoded).read()
jsonData = json.loads(rawData)
print "\n\n\n" + search_term + "\n\n\n"
searchResults = jsonData['responseData']['results']
print searchResults
for er in searchResults:
link = er['url']
if "www.linkedin.com" in link:
print link
file_out.write(lst[0] + " | " + lst[1] + " | " + link + "\n")
file_out.close()
运行它时,出现以下错误:
追溯(最近一次通话):
文件“ /Users/InNov8/Desktop/conference/delete.py”,第30行,在
searchResults = jsonData ['responseData'] ['results']
TypeError:“ NoneType”对象没有属性“ getitem”
我知道以前已经讨论过此主题,但是我是一个菜鸟,无法弄清楚如何对我的特定代码应用任何建议的修复程序。
谢谢。
最佳答案
jsonData ['responseData']返回None,因此您无法为其编制索引(jsonData ['responseData'] ['results'])
一种可能的解决方法是检查jsonData ['responseData']是否为None:
responseData = jsonData['responseData']
if responseData is not None:
searchResults = responseData['results']
print searchResults
for er in searchResults:
link = er['url']
if "www.linkedin.com" in link:
print link
file_out.write(lst[0] + " | " + lst[1] + " | " + link + "\n")
关于python - Python TypeError:“NoneType”对象的Google搜索没有属性“__getitem__”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22876148/