Search code examples
pythonpython-3.xindentation

I am having IndentationError Problem & unable to do proper Indentation


I am having a problem with Indentation in my python script

This is my Code

from lxml.html import fromstring
import requests
from itertools import cycle
import traceback
def get_proxies():
    url = 'https://free-proxy-list.net/'
    response = requests.get(url)
    parser = fromstring(response.text)
    proxies = set()
    for i in parser.xpath('//tbody/tr')[:10]:
        if i.xpath('.//td[7][contains(text(),"yes")]'):
           proxy = ":".join([i.xpath('.//td[1]/text()')[0], i.xpath('.//td[2]/text()')[0]])
           proxies.add(proxy)
    return proxies

    proxies = get_proxies()
    proxy_pool = cycle(proxies)
    url = 'https://httpbin.org/ip'
    for i in range(1,11):
#Get a proxy from the pool
      proxy = next(proxy_pool)  
      print("Request #%d"%i)
      try:
                response = requests.get(url,proxies={"http": proxy, "https": proxy})
                print(response.json())
      except:

                print("Skipping. Connnection error")

There isn't any problem with the code. I am just having trouble doing the proper Indentation.


Solution

  • You have to delete the space before proxies, and the same for the rest of your code:

    from lxml.html import fromstring
    import requests
    from itertools import cycle
    import traceback
    def get_proxies():
       url = 'https://free-proxy-list.net/'
       response = requests.get(url)
       parser = fromstring(response.text)
       proxies = set()
       for i in parser.xpath('//tbody/tr')[:10]:
           if i.xpath('.//td[7][contains(text(),"yes")]'):
              proxy = ":".join([i.xpath('.//td[1]/text()')[0], 
              i.xpath('.//td[2]/text()')[0]])
              proxies.add(proxy)
       return proxies
    
    proxies = get_proxies()
    proxy_pool = cycle(proxies)
    url = 'https://httpbin.org/ip'
    for i in range(1,11):
    #Get a proxy from the pool
       proxy = next(proxy_pool)  
       print("Request #%d"%i)
       try:
                response = requests.get(url,proxies={"http": proxy, "https": proxy})
                print(response.json())
       except:
    
                print("Skipping. Connnection error")