Есть html, например:
<div class="foo">
<a class="link" href="https://google.com">Неважно что тут</a>
</div>
Мне нужно получить только значение атрибута элемента, то есть: https://google.com
Или полностью весь html внутри элемента с классом foo (<a class="link" href="https://google.com">Неважно что тут</a>
)
Есть код, который получает текстовое содержимое элемента. Как мне его подправить для этой задачи?
def get_html (request):
return BeautifulSoup(request, 'lxml')
def save_file(text):
with open(output_filename, "a", encoding='utf8') as hello_file:
print(text, file=hello_file)
def parse(url):
r = request(url)
html = get_html(r)
items = html.select('div[class=foo]')
for item in items:
text = item.text
save_file(text)
Банальная замена text = item.text на text = item.html не работает.