找到元素by text,获取next font sibling并解析b标签中的事件列表,从previous sibling获取事件日期。
完整的工作代码:
from bs4 import BeautifulSoup
import requests
url = "http://sunsettheatre.com/"
response = requests.get(url)
soup = BeautifulSoup(response.content, "html5lib")
font = soup.find("b", text="Past Movies:").find_next_sibling("font")
for event in font.find_all("b", recursive=False):
event_date = event.previous_sibling.strip()
event_text = event.get_text(strip=True)
print(event_date, event_text)
打印:
(u'January 1, 2 & 3:', u'Alvin and the Chipmunks: The Road Chip')
(u'January 8, 9 & 10:', u"Daddy's Home")
(u'January 15, 16 & 17:', u'Star Wars: The Force Awakens')
(u'January 22, 23 & 24:', u'Star Wars: The Force Awakens 3D')
(u'January 29, 30 & 31:', u'Norm of the North')
(u'February 5, 6 & 7:', u'The Forest')
(u'February 12, 13 & 14', u'Kung Fu Panda 3')
(u'February 19, 20 & 21', u'Kung Fu Panda 3 3D')
(u'February 26, 27 & 28', u'Ride Along 2')
(u'March 4, 5 & 6', u'Deadpool')
(u'March 11, 12 & 13', u'Gods of Egypt')
(u'March 18, 19 & 20', u'Zootopia')
(u'March 25, 26 & 27', u'Zootopia 3D')
(u'April 1, 2 & 3', u'The Divergent Series: Allegiant')
(u'April 8, 9 & 10', u'Miracles From Heaven')
(u'April 29, 30 & May 1', u'Batman v Superman')