Hello everyone I’m having trouble while using BeautifulSoup , indeed i don’t succeed to access the information that I want, here is my code :
JavaScript
x
21
21
1
import nltk
2
3
import requests
4
5
from bs4 import BeautifulSoup
6
7
import time
8
9
from datetime import date, datetime, timedelta
10
11
url = 'https://www.richter-helm.eu/index.php?id=26'
12
13
page = requests.get(url)
14
15
soup = BeautifulSoup(page.content,'html.parser')
16
17
results_date=soup.find_all(class_='csc-textpic-text')[1]
18
19
20
print(results_date)
21
The output of this code is that :
JavaScript
1
6
1
</p> class="csc-textpic-text"><p class="bodytext">2021
2
</p>lass="bodytext">10. March 2021
3
</p>lass="bodytext">The NDR reports on the important role of Richter-Helm BioLogics as a Germany-based manufacturer of a DNA vaccine to support the Pharma industry and the fight against COVID-19. <a class="external-link-new-window" href="https://www.ndr.de/nachrichten/schleswig-holstein/Corona-In-Bovenau-wird-an-einem-neuen-Impfstoff-gearbeitet,bovenau124.html" target="_blank" title="Opens internal link in current window">See and read more</a> about our contribution to the fight against Corona.
4
<p class="bodytext"> </p>
5
</p>lass="bodytext">15. February 2021
6
and what I want is the second ‘p’ with the information : ’10. March 2021′ however I don’t know how to access this information, I tried : print(results_date.find(‘p’)) instead of print(results_date) but it gives me the first ‘p’ meaning it gives me: ‘p class=”bodytext”>2021’ and it’s stil not what I want, if anyone can help me it will be very nice.
Advertisement
Answer
You can use the :nth-of-type(n)
CSS Selector.
To use a CSS Selector, use the .select()
method instead of .find_all()
.
In your example, find the id="c51"
, and then the second p
tag.
JavaScript
1
9
1
import requests
2
from bs4 import BeautifulSoup
3
4
URL = "https://www.richter-helm.eu/index.php?id=26"
5
soup = BeautifulSoup(requests.get(URL).content, "html.parser")
6
7
# The following will select the `id="c51"`, and than the second `p` tag.
8
print(soup.select_one("#c51 p:nth-of-type(2)").text)
9
Output:
JavaScript
1
2
1
10. March 2021
2