0

у меня есть такой код:

def get_html_hidemy(site):
    r = requests.get(site)
    return r.text

def get_page_data_hidemy(html):
    soup = BeautifulSoup(html, 'lxml')
    line = soup.find_all('div', class_='table_block').find("table").find('tbody').find_all('tr')

    for tr in line:
        td = tr.find_all('td')
        ip = td[0].text
        port = td[1].text
        country = td[2].text.replace('\xa0', '')
        types = td[4].text.replace('\r\n\t\t\t\t\t', '').replace('\r\n      ', '')

        data = {'ip': ip,
                'Порт': port,
                'Страна': country,
                'Тип': types}

        print(data)

def hidemy():
    url_hidemy = 'https://hidemy.name/ru/proxy-list/#list'
    get_page_data_hidemy(get_html_hidemy(url_hidemy))


if __name__ == '__main__':
    hidemy()

и пишет ошибку:

Traceback (most recent call last):
  File "C:\Users\Иван\AppData\Local\Programs\Python\Python38\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Users\Иван\AppData\Local\Programs\Python\Python38\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Иван\Desktop\pypi\bot_bomber_admin.py", line 105, in hidemy
    get_page_data_hidemy(get_html_hidemy(url_hidemy))
  File "C:\Users\Иван\Desktop\pypi\bot_bomber_admin.py", line 79, in get_page_data_hidemy
    line = soup.find_all('table', class_='table_block').find('tbody').find_all('tr')
  File "C:\Users\Иван\AppData\Local\Programs\Python\Python38\lib\site-packages\bs4\element.py", line 2173, in __getattr__
    raise AttributeError(
AttributeError: ResultSet object has no attribute 'find'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?

что я делаю не так?

2
  • 1
    line = soup.find_all('div', class_='table_block') замените на line = soup.find('div', class_='table_block')
    – Danis
    21 апр 2021 в 7:13
  • @Danis line = soup.find('div', class_='table_block').find("table").find('tbody').find_all('tr') AttributeError: 'NoneType' object has no attribute 'find' 21 апр 2021 в 14:28

1 ответ 1

1

Лучше поучите bs4, так нельзя его использовать.
Вот код который решит вашу проблему и не укорачивайте код по максимуму, а то всё перестанет работать)

def get_html_hidemy(site):
    r = requests.get(site)
    return r.text

def get_page_data_hidemy(html):
    soup = BeautifulSoup(html, 'lxml')
    line = soup.find('div', class_='table_block')
    proxies = line.find_all("tr")

    for tr in proxies:
        td = tr.find_all('td')
        ip = td[0].text
        port = td[1].text
        country = td[2].text.replace('\xa0', '')
        types = td[4].text.replace('\r\n\t\t\t\t\t', '').replace('\r\n      ', '')

        data = {'ip': ip,
                'Порт': port,
                'Страна': country,
                'Тип': types}

        print(data)

def hidemy():
    url_hidemy = 'https://hidemy.name/ru/proxy-list/#list'
    get_page_data_hidemy(get_html_hidemy(url_hidemy))


if __name__ == '__main__':
    hidemy()
1
  • А чтобы не било никаких казусов ещё рекомендую место lxml использовать html.parser тоесть soup = BeautifulSoup(html, 'html.parser') 26 апр 2021 в 17:50

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.