#!/usr/bin/env python3
import requests, io
from lxml import etree as e
u = 'https://qna.habr.com/q/713045?e=8590853#comment_2122051'
t = 'class'
v = 'question__title'
def l(u: str, t: str, v: str) -> str:
p = e.HTMLParser()
h = requests.get(u).text
r = e.parse(io.StringIO(h), p)
return r.xpath(f'.//*[@{t}="{v}"]')[0].text
print(l(u, t, v))
Как получить данные из HTML тега с помощью Python?
usr@Inspiron-5749:~/Development/Python$ ./lxml-parser.py
Traceback (most recent call last):
File "./lxml-parser.py", line 14, in <module>
print(l(u, t, v))
File "./lxml-parser.py", line 12, in l
return r.xpath(f'.//*[@{t}="{v}"]')[0].text
IndexError: list index out of range