【发布时间】:2019-06-03 05:33:18
【问题描述】:
我一直在尝试使用 BeautifulSoup,因为我想尝试抓取网页 (https://www.imdb.com/search/title?release_date=2017&sort=num_votes,desc&page=1)。到目前为止,我已经成功地抓取了一些元素,但现在我想抓取电影说明,但我一直在苦苦挣扎。描述在 html 中的位置是这样的:
<div class="lister-item mode-advanced">
<div class="lister-item-content>
<p class="muted-text"> paragraph I don't need</p>
<p class="muted-text"> paragraph I need</p>
</div>
</div>
我想抓取似乎很容易做到的第二段,但我尝试的所有内容都给了我“无”作为输出。我一直在四处寻找答案。在另一个 stackoverflow 帖子中,我发现
find('p:nth-of-type(1)')
或
find_elements_by_css_selector('.lister-item-mode >p:nth-child(1)')
可以做到这一点,但它仍然给了我
none #as output
你可以在下面找到我的一段代码,它的等级有点低,因为我只是在尝试学习的东西
import urllib2
from bs4 import BeautifulSoup
from requests import get
url = 'http://www.imdb.com/search/title?
release_date=2017&sort=num_votes,desc&page=1'
response = get(url)
html_soup = BeautifulSoup(response.text, 'html.parser')
type(html_soup)
movie_containers = html_soup.find_all('div', class_='lister-item mode-
advanced')
first_movie = movie_containers[0]
first_title = first_movie.h3.a.text
print first_title
first_year = first_movie.h3.find('span', class_='lister-item-year text-muted unbold')
first_year = first_year.text
print first_year
first_imdb = float(first_movie.strong.text)
print first_imdb
# !!!! problem zone ---------------------------------------------
first_description = first_movie.find('p', class_='muted-text')
#first_description = first_description.text
print first_description
上面的代码给了我这个输出:
$ python scrape.py
Logan
(2017)
8.1
None
我想学习选择html标签的正确方法,因为这对以后的项目很有用。
【问题讨论】:
标签: python html beautifulsoup