1

The following is part of a website that I am trying to extract the video titles from:

</div>
<div class="yt-lockup-content">
        <h3 class="yt-lockup-title">
<a class="yt-uix-sessionlink yt-uix-tile-link yt-uix-contextlink 
      yt-ui-ellipsis yt-ui-ellipsis-2"
    dir="ltr"
      title="Harder Polynomials"
    data-sessionlink="ei=fYsHUvSLA8uzigLq74CABQ&amp;ved=CB8Qvxs&amp;feature=c4-videos-u"
    href="/watch?v=LHvQeBRLFn8"
  >
    Harder Polynomials
</a>

I wish to extract the video title (Harder Polynomials) from this. I have tried the following code:

import requests
from bs4 import BeutifulSoup

resp=requests.get('http://www.youtube.com/user/sachinabey/videos')

a=soup.findAll('a', attrs={'class': 'yt-uix-sessionlink yt-uix-tile-link yt-uix-  contextlink yt-ui-ellipsis yt-ui-ellipsis-2'})

a is empty, what am I doing wrong. From here how do I extract the title

4

2 回答 2

0

这是从页面打印所有视频标题的有效解决方案:

import requests
from bs4 import BeautifulSoup

resp = requests.get('http://www.youtube.com/user/sachinabey/videos')

soup = BeautifulSoup(resp.text)
for title in soup.findAll('h3', attrs={'class': 'yt-lockup-title'}):
    print title.find('a').text.strip()

它打印:

Harder Polynomials
Summing tan inverse
iGraph tutorial
Integrate e^(-x^2)
Chord of Contact to Ellipse
Equation of Tangents of an Ellipse or Hyperbola
Motion and Air Resistance
Projectile Motion
Regression in R
EM Algorithm Derivation
Cosine Rule proof
R writing functions
Proof of square root 2 being irrational
R for loops and while loops
Chi Squared Hypothesis Testing
Integration of Trignometric Functions
Sequences and Series Examples
ARCH GARCH Model Motivation
Integration by Parts
Differentiate Inverse Trigonometry
Simple Harmonic Motion Examples Part II
Simple Harmonic Motion Examples Part I
Simple Harmonic Motion -  Introduction
HSC Solutions 2009 3 Unit Q4
HSC 3 Unit Solutions 2009 Q2
HSC 3 Unit Maths 2009 Solutions
Parallel For Loops
Change of Base for Logarithms - Examples
Divisibility by 3 or 9
Multiplying by 11
于 2013-08-11T14:02:14.050 回答
0

我认为错误在于yt-uix- contextlink. 我认为这应该是一个错字。如果纠正它,它的工作原理。

演示:

>>> s
'<div class="yt-lockup-content">\n        <h3 class="yt-lockup-title">\n<a class="yt-uix-sessionlink yt-uix-tile-link yt-uix-contextlink \n      yt-ui-ellipsis yt-ui-ellipsis-2"\n    dir="ltr"\n      title="Harder Polynomials"\n    data-sessionlink="ei=fYsHUvSLA8uzigLq74CABQ&amp;ved=CB8Qvxs&amp;feature=c4-videos-u"\n    href="/watch?v=LHvQeBRLFn8"\n  >\n    Harder Polynomials\n</a>'
>>> soup=BeautifulSoup(s)
>>> soup.findAll('a', attrs={'class': 'yt-uix-sessionlink yt-uix-tile-link yt-uix-contextlink yt-ui-ellipsis yt-ui-ellipsis-2'})
[<a class="yt-uix-sessionlink yt-uix-tile-link yt-uix-contextlink yt-ui-ellipsis yt-ui-ellipsis-2" data-sessionlink="ei=fYsHUvSLA8uzigLq74CABQ&amp;ved=CB8Qvxs&amp;feature=c4-videos-u" dir="ltr" href="/watch?v=LHvQeBRLFn8" title="Harder Polynomials">
    Harder Polynomials
</a>]

或者,也许您可​​以传入课程列表。

>>> soup.findAll('a', attrs={'class': ['yt-uix-sessionlink', 'yt-uix-tile-link', 'yt-uix-contextlink',  'yt-ui-ellipsis yt-ui-ellipsis-2']})
[<a class="yt-uix-sessionlink yt-uix-tile-link yt-uix-contextlink yt-ui-ellipsis yt-ui-ellipsis-2" data-sessionlink="ei=fYsHUvSLA8uzigLq74CABQ&amp;ved=CB8Qvxs&amp;feature=c4-videos-u" dir="ltr" href="/watch?v=LHvQeBRLFn8" title="Harder Polynomials">
    Harder Polynomials
</a>]
于 2013-08-11T14:05:14.250 回答