Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 959995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:06:42+00:00 2026-05-16T01:06:42+00:00

This is my code: from xgoogle.search import GoogleSearch, SearchError import urllib, urllib2, sys, argparse

  • 0

This is my code:

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()

I’ve shorten it a little to make it easier to read, but it should still be functional. This code will be part of a bigger script.

I am using this lib: XGOOGLE to scrape the results from google, and then I visit each result to search if the website contains any of the strings from stringArr.

I made the first tests without any problem (I ctrl+C it after less than 10 results), but the first time I let it run, after about 100 urls tested I got this error:

  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

(lines numbers are not the same because I modified the code to post it here)

After that I got back my linux terminal like if the script has finished. But I noticed my pc wasn’t working quite well, I checked System Monitor and I saw the process Python using 1.3gb of memory, I had to kill the process to get back my pc to normal.

Is it something in my code that is causing this or why could it happen?

I know my code could have some errors, but right now I am mainly interested in any error that could be causing the memory problem. Any help will be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:06:43+00:00Added an answer on May 16, 2026 at 1:06 am

    I refactored your code a little to make it easier for me to read. I can’t see anything here that would leak memory though

    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()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.