这是我的代码:
from xgoogle.search import GoogleSearch, SearchError
import urllib, urllib2, sys, argparse
global stringArr
stringArr = ["string 1",
"string 2",
"string 3",
"string etc"]
def searchIt(url):
try:
if(args.verbose>='1'): print "[INFO] Opening URL: "+url
response = urllib.urlopen(url)
except urllib2.URLError, e:
print "[ERROR] "+e.reason
return False
except KeyboardInterrupt:
print "Suspended by user..."
sys.exit()
if(checkForStr(response.read())):
if(args.verbose=='0'): print "[INFO] String found in URL: "+url
else:
if(args.verbose>='1'): print "[INFO] No string found in URL: "+url
def checkForStr(html):
global stringArr
try:
if any(checkStr in html for checkStr in stringArr):
return True
else:
return False
except KeyboardInterrupt:
print "Suspended by user..."
sys.exit()
def main():
try:
i=0
gs = GoogleSearch(args.keyword)
gs.results_per_page = 100
results = []
while True:
tmp = gs.get_results()
i = i+1 # page number
if not tmp: # no more results (pages) were found
break
results.extend(tmp)
for r in results: # process results for page
searchIt(r.url) # check for string
del results[:] # clean results
# finished
except SearchError, e:
print "[ERROR] Search failed: %s" % e
except KeyboardInterrupt:
print "Suspended by user..."
sys.exit()
if __name__ == '__main__':
try:
parser = argparse.ArgumentParser()
parser.add_argument('-v', dest='verbose', default='0', help='Verbosity level', choices='012')
group = parser.add_argument_group('required arguments')
group.add_argument('-k', dest='keyword', help='Keyword to use on google query', required=True)
args = parser.parse_args()
main()
except KeyboardInterrupt:
print "Suspended by user..."
sys.exit()
我缩短了一点以使其更容易阅读,但它应该仍然有效。此代码将成为更大脚本的一部分。
我正在使用此lib:XGOOGLE来搜索google的结果,然后我访问每个结果以搜索该网站是否包含来自stringArr的任何字符串。
我做了第一次测试没有任何问题(我在不到10个结果后ctrl + C),但是第一次让它运行,经过大约100个urls测试后我得到了这个错误:
File "./StringScan.py", line 99, in <module>
main()
File "./StringScan.py", line 83, in main
checkForStr(r.url)
File "./StringScan.py", line 39, in checkForStr
response = urllib.urlopen(url)
File "/usr/lib/python2.6/urllib.py", line 86, in urlopen
return opener.open(url)
File "/usr/lib/python2.6/urllib.py", line 205, in open
return getattr(self, name)(url)
File "/usr/lib/python2.6/urllib.py", line 344, in open_http
h.endheaders()
File "/usr/lib/python2.6/httplib.py", line 904, in endheaders
self._send_output()
File "/usr/lib/python2.6/httplib.py", line 776, in _send_output
self.send(msg)
File "/usr/lib/python2.6/httplib.py", line 735, in send
self.connect()
File "/usr/lib/python2.6/httplib.py", line 716, in connect
self.timeout)
File "/usr/lib/python2.6/socket.py", line 500, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
IOError: [Errno socket error] [Errno -2] Name or service not known
(行号不一样,因为我修改了代码以便在此处发布)
之后我恢复了我的linux终端,就像脚本已经完成一样。但是我注意到我的电脑工作效果不好,我检查了系统监视器,我看到了使用1.3gb内存的Python进程,我不得不杀死进程让我的电脑恢复正常。
我的代码中是否存在导致此问题的原因或为什么会发生?
我知道我的代码可能有一些错误,但是现在我主要对可能导致内存问题的任何错误感兴趣。任何帮助将不胜感激。
答案 0 :(得分:0)
我重新编写了一些代码,让我更容易阅读。我在这里看不到任何会泄漏记忆的东西
from itertools import count
import urllib, urllib2, sys, argparse
from xgoogle.search import GoogleSearch, SearchError
stringArr = ["string 1",
"string 2",
"string 3",
"string etc"]
def searchIt(url):
try:
if(args.verbose>='1'):
print "[INFO] Opening URL: "+url
response = urllib.urlopen(url)
except urllib2.URLError, e:
print "[ERROR] "+e.reason
return False
if checkForStr(response.read()):
if(args.verbose=='0'):
print "[INFO] String found in URL: "+url
else:
if(args.verbose>='1'):
print "[INFO] No string found in URL: "+url
def checkForStr(html):
return any(checkStr in html for checkStr in stringArr)
def main():
try:
gs = GoogleSearch(args.keyword)
gs.results_per_page = 100
for i in count():
results = gs.get_results()
if not results: # no more results (pages) were found
break
for r in results: # process results for page
searchIt(r.url) # check for string
# finished
except SearchError, e:
print "[ERROR] Search failed: %s" % e
if __name__ == '__main__':
try:
parser = argparse.ArgumentParser()
parser.add_argument('-v', dest='verbose', default='0', help='Verbosity level', choices='012')
group = parser.add_argument_group('required arguments')
group.add_argument('-k', dest='keyword', help='Keyword to use on google query', required=True)
args = parser.parse_args()
main()
except KeyboardInterrupt:
print "Suspended by user..."
sys.exit()
答案 1 :(得分:0)
可能是urllib.urlopen()。见http://bugs.python.org/issue1208304