Python:BeautifulSoup提取div部分的所有跨度小节 [英] Python: BeautifulSoup extract all the span clases from div section

查看:51
本文介绍了Python:BeautifulSoup提取div部分的所有跨度小节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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)

我正在尝试在类"col-md-4 col-sm-4 side-box well"上打印第二个span类side-boxTop",但我无法打印该类的第二个跨度类(第二个P标签(事件日期),因为每个跨度类没有唯一的跨度名称

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)

部分输出:

24/01/2018
30/01/2018
31/01/2018
31/01/2018

这篇关于Python:BeautifulSoup提取div部分的所有跨度小节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆