BeautifulSoup:来自www.themoviedb.org的标题

时间:2017-06-24 23:47:20

标签: python web-scraping beautifulsoup

我知道这是具体的,但我希望找到一种方法来抓取以下网站:

https://www.themoviedb.org/discover/movie?page=1

并返回电影标题列表。

我尝试过BeautifulSoup:

from bs4 import BeautifulSoup
import requests

r = requests.get('https://www.themoviedb.org/discover/movie?page=1')

soup = BeautifulSoup(r.text)
soup

但是我在输出中找不到任何标题。我是新手,但我想知道是否有人可以提供一个如何做到这一点的例子?

2 个答案:

答案 0 :(得分:1)

查看HTML,似乎有关电影的信息位于<div>内,且类info

from bs4 import BeautifulSoup
import requests

r = requests.get('https://www.themoviedb.org/discover/movie?page=1')

soup = BeautifulSoup(r.text, "html5lib")
items = soup.find_all('div', {'class' : 'info'})

for item in items:
    print(item.p.a['title'])

输出:

Split
Miss Peregrine's Home for Peculiar Children
Deadpool
Captain America: Civil War
X-Men: Apocalypse
Fantastic Beasts and Where to Find Them
Arrival
Tomorrow Everything Starts
Doctor Strange
La La Land
Sing
The Great Wall
Rogue One: A Star Wars Story
Batman v Superman: Dawn of Justice
Hacksaw Ridge
Zootopia
Inferno
Star Trek Beyond
Now You See Me 2
Passengers

答案 1 :(得分:0)

这应该让你非常接近:

for x in soup.find_all('div', {'class': 'item poster card'}): 
    print list(x.find_all('a')[1])