简体   繁体   中英

Python: BeautifulSoup extract all the span clases from div section

from requests import get
from bs4 import BeautifulSoup

url = 'https://www.ceda.com.au/Events/Upcoming-events'

response = get(url)

events_container = html_soup.find_all('div', class_ = 'list-bx')

event1name = events_container[0]

print(event1name.a.text)

Eventdate = html_soup.find('div', class_ = ' col-md-4 col-sm-4 side-box well 
side-boxTop')

x = Eventdate.div.text
print(x)

I'm trying to print the second span class on the class " col-md-4 col-sm-4 side-box well side-boxTop" But I coud'nt able to print the second span class (Second P tag (Event Date) from the class as there is no unique span name for the each span class

Try this. It will fetch you the date you are after:

from requests import get
from bs4 import BeautifulSoup

res = get('https://www.ceda.com.au/Events/Upcoming-events')
soup = BeautifulSoup(res.text,"lxml")
item_date = '\n'.join([' '.join(item.find_parent().select("span")[0].text.split()) for item in soup.select(".side-list .icon-calendar")])
print(item_date)

Partial output:

24/01/2018
30/01/2018
31/01/2018
31/01/2018
from requests import get
from bs4 import BeautifulSoup
url = 'https://www.ceda.com.au/Events/Upcoming-events'
response = get(url)
html_soup=BeautifulSoup(response.content,"lxml")

events_container = html_soup.find_all('div', class_ = 'list-bx')

event1name = events_container[0]

print(event1name.a.text)

Eventdate = html_soup.find('div', class_ = ' col-md-4 col-sm-4 side-box well side-boxTop')
date=Eventdate.find_all("p")[1].text
print(date)

You can apply find_all to parent as well so you can just use find_all and navigate to any node you want.

Now you can just edit your date by textManipulation or so.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM