BeautifulSoup传递div

时间:2015-01-07 21:33:36

标签: python html web-scraping beautifulsoup html-parsing

我想传递这个页面:

http://animedigitalnetwork.fr/video/naruto-shippuden

我测试了这个:

import urllib2
from BeautifulSoup import BeautifulSoup

page = urllib2.urlopen('http://animedigitalnetwork.fr/video/naruto-shippuden')
soup = BeautifulSoup(page)
first_div = soup.find('div',{"class" : "adn-video"})

结果不是火影忍者!

<div class="adn-video"> <div class="adn-video_screenshot">
<img src="http://image.animedigitalnetwork.fr/license/claymore/tv/web/eps1_328x184.jpg" alt="Claymore 1" /><span class="adn_video_play-button"></span> </div><div class="adn-video_text"><div class="adn-video_title">
<h4>Claymore</h4><span>Épisode 1</span><div class="adn-rating mobile-hide" itemprop="aggregateRating" itemscope="itemscope" itemtype="http://schema.org/AggregateRating"><meta itemprop="ratingValue" content="4.6667" /><meta itemprop="ratingCount" content="10" /><div id="adn-rating"><ul class="adn-rating_empty"><li>&#xe002;</li><li>&#xe002;</li><li>&#xe002;</li><li>&#xe002;</li><li>&#xe002;</li></ul><ul class="adn-rating_rating"><li>&#xe002;</li><li>&#xe002;</li><li>&#xe002;</li><li>&#xe002;</li><li>&#xe002;</li></ul></div><p class="adn-rating-message"></p></div></div><div class="adn-video_link">
<a title="Claymore 1" href="/video/claymore/1849-episode-1-la-claymore">Voir la vidéo</a>
</div></div></div>

1 个答案:

答案 0 :(得分:0)

这里有多个问题:

  • 在形成视频列表时还有一个额外的异步调用:
    • 首先,您需要从初始页面
    • 中提取playlist参数值
    • 然后,发布帖子请求以将视频发送到this url
  • 您使用BeautifulSoup pip install beautifulsoup4 - 切换到outdated and not maintained version

    urllib2
  • 使用bs4代替from bs4 import BeautifulSoup import requests headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'} # initialize session session = requests.Session() # getting playlist response = session.get('http://animedigitalnetwork.fr/video/naruto-shippuden', headers=headers) soup = BeautifulSoup(response.content) playlist = soup.find('a', {'data-playlist': True})['data-playlist'] # getting list of videos url = 'http://animedigitalnetwork.fr/index.php?option=com_vodvideo&view=playlist&format=raw' response = session.post(url, data={ 'playlist': playlist, 'season': '', 'order': 'DESC' }, headers=headers) soup = BeautifulSoup(response.content) for video in soup.select('div.adn-video'): print video.a.get('title') ,维护requests并传递请求标题

完整的工作代码:

Naruto Shippuden 391
Naruto Shippuden 390
Naruto Shippuden 389
...
Naruto Shippuden 3
Naruto Shippuden 2
Naruto Shippuden 1

打印(视频节目列表):

{{1}}