I wanna get all the p tags and store it in a list, but unfortunately all of them have a
between.
This is how the content looks like:
<p>Ich halt mir die Pistole an den Kopf
<br/>Doch drück' nicht ab, denn ich hab zu viel Angst vor Gott</p>,
<p>Feinde wurden zu Brüdern
<br/>Und Brüder wurden zu V-Männern
<br/>Die beste Gang, in der ich jemals war
<br/>Me, myself und meine DNA</p>,
and i should look like:
[Ich halt mir die Pistole an den Kopf
Doch drück' nicht ab, denn ich hab zu viel Angst vor Gott, Feinde wurden zu Brüdern
Und Brüder wurden zu V-Männern
Die beste Gang, in der ich jemals war
Me, myself und meine DNA,]
Thats my current code:
url = requests.get("https://www.myzitate.de/suche/farid-bang/")
z = bs(url.content)
cont = z.find("div", attrs={"id":"cont"})
cont.find_all("p")
Is this what you're looking for?
ps = [p.get_text() for p in cont.find_all("p")]
You can use tag.get_text()
with parameters strip=True
and separator='\n'
to get correct text:
import requests
from bs4 import BeautifulSoup
url = 'https://www.myzitate.de/suche/farid-bang/'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')
for p in soup.select('p'):
print(p.get_text(strip=True, separator='\n'))
print('-' * 80)
Prints:
Ich halt mir die Pistole an den Kopf
Doch drück' nicht ab, denn ich hab zu viel Angst vor Gott
--------------------------------------------------------------------------------
Feinde wurden zu Brüdern
Und Brüder wurden zu V-Männern
Die beste Gang, in der ich jemals war
Me, myself und meine DNA
--------------------------------------------------------------------------------
Wir sind Public Enemy
Ihr Police Academy
Kann nicht tanzen, meine Schultern sind zu breit
Du willst mich batteln und frisst dann den Bürgersteig
--------------------------------------------------------------------------------
Wir kam'n von unten mit Gangsta-Rap
Und sind im Endeffekt drei Gs wie das Handynetz
--------------------------------------------------------------------------------
...and so on.
All the items are located under the id
of z
which is accessible via
page_soup.find_all('div', {'id': 'z'}):
In some website, the are multiple instances where the p tag exist, so I usually will search for upper class just to make sure I get the intended output.
The text you interested are within the p tag
and retrievable via
div_tag.find_all('p')
The full code is as below:
append_text=[]
url = requests.get("https://www.myzitate.de/suche/farid-bang/")
page_soup= Soup(url.content)
for div_tag in page_soup.find_all('div', {'id': 'z'}):
for litag in div_tag.find_all('p'):
append_text.append(litag.text)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.