Python: BeautifulSoup extract all the span clases from div section

帅比萌擦擦* 提交于 2019-12-23 01:58:10

问题


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


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/47912149/python-beautifulsoup-extract-all-the-span-clases-from-div-section

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!