Python 使用BeautifulSoup解析IMDB

Python 使用BeautifulSoup解析IMDB,python,beautifulsoup,imdb,Python,Beautifulsoup,Imdb,我使用BeautifulSoup和Python2.7从IMDB的移动站点上剥离了以下代码 我想为第1集、标题“冬天来了”和IMDB分数“8.9”创建一个单独的对象。我似乎不知道该如何分割集号和标题 <a class="btn-full" href="/title/tt1480055?ref_=m_ttep_ep_ep1"> <span class="text-large"> 1. <strong> Wint

我使用BeautifulSoup和Python2.7从IMDB的移动站点上剥离了以下代码

我想为第1集、标题“冬天来了”和IMDB分数“8.9”创建一个单独的对象。我似乎不知道该如何分割集号和标题

   <a class="btn-full" href="/title/tt1480055?ref_=m_ttep_ep_ep1">
     <span class="text-large">
      1.
      <strong>
       Winter Is Coming
      </strong>
     </span>
     <br/>
     <span class="mobile-sprite tiny-star">
     </span>
     <strong>
      8.9
     </strong>
     17 Apr. 2011
    </a>
可以使用“查找”来定位跨度,该跨度的类文本比所需的特定元素大

一旦你有了你想要的广度,你可以使用next来抓取下一行,其中包含剧集编号,并查找包含标题的强项

html = """
<a class="btn-full" href="/title/tt1480055?ref_=m_ttep_ep_ep1">
     <span class="text-large">
      1.
      <strong>
       Winter Is Coming
      </strong>
     </span>
     <br/>
     <span class="mobile-sprite tiny-star">
     </span>
     <strong>
      8.9
     </strong>
     17 Apr. 2011
    </a>
"""

from bs4 import BeautifulSoup

soup = BeautifulSoup(html)
span = soup.find('span', attrs={'text-large'})
ep = str(span.next).strip()
title = str(span.find('strong').text).strip()

print ep
print title

> 1. 
> Winter Is Coming
可以使用“查找”来定位跨度,该跨度的类文本比所需的特定元素大

一旦你有了你想要的广度,你可以使用next来抓取下一行,其中包含剧集编号,并查找包含标题的强项

html = """
<a class="btn-full" href="/title/tt1480055?ref_=m_ttep_ep_ep1">
     <span class="text-large">
      1.
      <strong>
       Winter Is Coming
      </strong>
     </span>
     <br/>
     <span class="mobile-sprite tiny-star">
     </span>
     <strong>
      8.9
     </strong>
     17 Apr. 2011
    </a>
"""

from bs4 import BeautifulSoup

soup = BeautifulSoup(html)
span = soup.find('span', attrs={'text-large'})
ep = str(span.next).strip()
title = str(span.find('strong').text).strip()

print ep
print title

> 1. 
> Winter Is Coming
一旦每个a class=btn都满了,就可以使用span类来获取所需的标记,strong标记是span的子类,文本为large类,因此只需在标记上调用.strong.text,对于css类mobile sprite tiny star的span,您需要查找下一个强标记,因为它是span的同级,而不是子级:

h = """<a class="btn-full" href="/title/tt1480055?ref_=m_ttep_ep_ep1">
     <span class="text-large">
      1.
      <strong>
       Winter Is Coming
      </strong>
     </span>
     <br/>
     <span class="mobile-sprite tiny-star">
     </span>
     <strong>
      8.9
     </strong>
     17 Apr. 2011
    </a>
"""

from bs4 import BeautifulSoup

soup = BeautifulSoup(h)
title = soup.select_one("span.text-large").strong.text.strip()
score = soup.select_one("span.mobile-sprite.tiny-star").find_next("strong").text.strip()

print(title, score)
如果你真的想得到这一集,最简单的方法是将文本拆分一次:

soup = BeautifulSoup(h)
ep, title = soup.select_one("span.text-large").text.split(None, 1)
score = soup.select_one("span.mobile-sprite.tiny-star").find_next("strong").text.strip()

print(ep, title.strip(), score)
这将给你:

(u'1.', u'Winter Is Coming', u'8.9')
一旦每个a class=btn都满了,就可以使用span类来获取所需的标记,strong标记是span的子类,文本为large类,因此只需在标记上调用.strong.text,对于css类mobile sprite tiny star的span,您需要查找下一个强标记,因为它是span的同级,而不是子级:

h = """<a class="btn-full" href="/title/tt1480055?ref_=m_ttep_ep_ep1">
     <span class="text-large">
      1.
      <strong>
       Winter Is Coming
      </strong>
     </span>
     <br/>
     <span class="mobile-sprite tiny-star">
     </span>
     <strong>
      8.9
     </strong>
     17 Apr. 2011
    </a>
"""

from bs4 import BeautifulSoup

soup = BeautifulSoup(h)
title = soup.select_one("span.text-large").strong.text.strip()
score = soup.select_one("span.mobile-sprite.tiny-star").find_next("strong").text.strip()

print(title, score)
如果你真的想得到这一集,最简单的方法是将文本拆分一次:

soup = BeautifulSoup(h)
ep, title = soup.select_one("span.text-large").text.split(None, 1)
score = soup.select_one("span.mobile-sprite.tiny-star").find_next("strong").text.strip()

print(ep, title.strip(), score)
这将给你:

(u'1.', u'Winter Is Coming', u'8.9')

使用url html抓取与reguest和正则表达式搜索

import os, sys, requests

frame = ('http://www.imdb.com/title/tt1480055?ref_=m_ttep_ep_ep1')
f = requests.get(frame)
helpme = f.text
import re
result = re.findall('itemprop="name" class="">(.*?)&nbsp;', helpme)
result2 = re.findall('"ratingCount">(.*?)</span>', helpme)
result3 = re.findall('"ratingValue">(.*?)</span>', helpme)
print result[0].encode('utf-8')
print result2[0]
print result3[0]

使用url html抓取与reguest和正则表达式搜索

import os, sys, requests

frame = ('http://www.imdb.com/title/tt1480055?ref_=m_ttep_ep_ep1')
f = requests.get(frame)
helpme = f.text
import re
result = re.findall('itemprop="name" class="">(.*?)&nbsp;', helpme)
result2 = re.findall('"ratingCount">(.*?)</span>', helpme)
result3 = re.findall('"ratingValue">(.*?)</span>', helpme)
print result[0].encode('utf-8')
print result2[0]
print result3[0]

我去回答你的问题,但后来意识到我有点误解了。如果所有的剧集都是这样设置的,你可以通过做一些像“汤”这样的事情来获取标题。选择“span>strong”,但这仍然让你需要获取剧集号。冬天来了。。。和权力游戏双关语:我去回答你的问题,但后来意识到我有点误解了。如果所有的剧集都是这样设置的,你可以通过做一些像“汤”这样的事情来获取标题。选择“span>strong”,但这仍然让你需要获取剧集号。冬天来了。。。和权力双关语的线索游戏:D