-2

This is my code and it is always priting 'something wrong' which I assigned if url does not open . How could I make my code more effective in terms of memory usage and time because my code is taking forever to execute and printing nothing more than 'something wrong'. I will end my data I am working on if anyone is interested

    import requests
import re
import urllib2
import time
from bs4 import BeautifulSoup
iteration=0
a=0
b=0
links=list()
links2=list()
emails=dict()
while (iteration<1):
     a=b
     b=a+2
     links2=links[a:b]
     def extract_emails(links2):
         for url in links2:
             try:
                 response=requests.get(url)
                 if response.status_code!=200:
                     print 'connection refused'
                 else:
                     contents=requests.get(url).content.decode('utf-8')
                     emails[url]= re.findall(r'[\w\.-]+@[\w\.-]+',contents)
             except Exception as e: 
                 print(e)
         return emails
     def main():
         extract_links(r)
         extract_emails(extract_links(r))
     main()
     iteration=iteration+1
Aditya Matturi
  • 119
  • 1
  • 7

1 Answers1

2

You wrote while (iteration<1): and never assign any value to iteration after initialization with 0, and you never broke the while loop, obviously your script will never terminate !

Mehdi Benmoha
  • 3,694
  • 3
  • 23
  • 43
  • Thank you for fixing part of my problem but still my code is printing 'something wrong' – Aditya Matturi Dec 21 '17 at 16:45
  • You better print the exception for debuging rather that the string 'something wrong', check this to know how to print an exception: https://stackoverflow.com/questions/1483429/how-to-print-an-error-in-python – Mehdi Benmoha Dec 21 '17 at 16:49