Home > Software design >  Getting error after declaration of a function "IndentationError: expected an indented block&quo
Getting error after declaration of a function "IndentationError: expected an indented block&quo

Time:10-30

I have written this code and got an error

from lxml.html import fromstring
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")]'):
#Grabbing IP and corresponding PORT
    proxy = ":".join([i.xpath('.//td[1]/text()')[0], i.xpath('.//td[2]/text()')[0]])
proxies.add(proxy)
    return proxies

And the Error is

  File "check.py", line 13
    return proxies
    ^
IndentationError: unexpected indent

Unable to understand the error but maybe the line spacing has something to do with this

CodePudding user response:

You have poor indentation.

import requests
from lxml.html import fromstring

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")]'):
            #Grabbing IP and corresponding PORT
            proxy = ":".join([i.xpath('.//td[1]/text()')[0], i.xpath('.//td[2]/text()')[0]])
            proxies.add(proxy)
    return proxies

CodePudding user response:

Python is senstive to tabs and you forget to import requests package:

from lxml.html import fromstring
import requests

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")]'):
        #Grabbing IP and corresponding PORT
            proxy = ":".join([i.xpath('.//td[1]/text()')[0], i.xpath('.//td[2]/text()')[0]])
            proxies.add(proxy)
    return proxies
  • Related