使用BeautifulSoup在网页上查找特定文本

Nat*_*n R 5 html python beautifulsoup web-scraping python-3.x

我正在尝试使用Python 3和Beautiful Soup 4保存网站上的电影列表。问题是,我对Python和BS还是很陌生,我真的不知道从哪里开始。

网站是http://sunsettheatre.com,电影列表在“过去的电影:”之后。我不知道如何提取该块。我一直在使用Google搜索,似乎“美丽汤”在尝试查找标签时效果最好,但是我只需要它来查找一个文本列表,该列表中没有任何特定的标签(该网站并非专业设计的)。

有什么方法可以使Beautiful Soup和Python提取“过去的电影:”和“有关我们播放的电影的完整列表,请单击此处”之间的文本?

ale*_*cxe 5

找到元素的文本,得到了下一个font兄弟和解析在事件列表中b得到从事件日期标记上一个兄弟

完整的工作代码:

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)
Run Code Online (Sandbox Code Playgroud)

印刷品:

(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')
Run Code Online (Sandbox Code Playgroud)