J
J
jbuser2019-07-22 18:48:08
Python
jbuser, 2019-07-22 18:48:08

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)

Answer:
Connecting.. ОК
<div class="titlecard"> <strong><a href="/196/58235/navstrechu-obshchestvennym-5-avgusta">НАВСТРЕЧУ ВСЕМ К 12.00</a></strong> </div>

Now how to pull out "MEET ALL BY 12.00" from the result?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Taus, 2019-07-22
@Taus

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

J
jbuser, 2019-07-22
@jbuser

link = soup.find('strong')
Answer:

<strong><a href="/196/58235/navstrechu-obshchestvennym-5-avgusta">НАВСТРЕЧУ ВСЕМ К 12.00</a></strong>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question