Answer the question
In order to leave comments, you need to log in
Python bs4 get text from link?
Friends, hello everyone!
He graduated from one university (technical) and went to software engineering. The first year at the university will be easy, so I decided to choose python as the main language for writing code.
I decided to learn from simple tasks, and one of them is the parse method using the BeautifulSoup libraries
Code:
import requests
from bs4 import BeautifulSoup
html = requests.get('http://site.ru') # get-запрос
if html.status_code == 200:
print('Connecting.. ОК')
else:
print('Connecting.. Error')
soup = BeautifulSoup(html.text,'html.parser')
link = soup.find('div', class_='titlecard')
print (link)
Connecting.. ОК
<div class="titlecard"> <strong><a href="/196/58235/navstrechu-obshchestvennym-5-avgusta">НАВСТРЕЧУ ВСЕМ К 12.00</a></strong> </div>
Answer the question
In order to leave comments, you need to log in
Read the documentation more carefully. You can chain find . To get text from .string or .contents tags . If there are no other tags inside <a></a>
, then like this:
link = soup.find('div', class_='titlecard').find('a')
link_content = link.string
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question