【问题标题】:BeautifulSoup: scraping titles from www.themoviedb.orgBeautifulSoup:从 www.themoviedb.org 抓取标题
【发布时间】:2017-06-24 23:47:19
【问题描述】:
我知道这是特定的,但我正在寻找一种方法来抓取以下网站:
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
但是我在输出中找不到任何标题。我是新手,但我想知道是否有人可以提供一个示例来说明您将如何做到这一点?
【问题讨论】:
标签:
python
web-scraping
beautifulsoup
【解决方案1】:
查看 HTML,似乎有关电影的信息位于 <div>s 中,类为 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
【解决方案2】:
这应该让你非常接近:
for x in soup.find_all('div', {'class': 'item poster card'}):
print list(x.find_all('a')[1])