BeautifulSoup 网络爬虫:如何获取一段文字
BeautifulSoup webcrawling: How to get piece of text
我要抓取的页面是 http://www.boxofficemojo.com/yearly/chart/?page=1&view=releasedate&view2=domestic&yr=2013&p=.htm. Specifically, I am focusing on this page right now: http://www.boxofficemojo.com/movies/?id=ironman3.htm。
对于第一部 link 上的每一部电影,我想获得类型、放映时间、MPAA 评级、国外票房收入和预算。我很难得到这个,因为信息上没有识别标签。我目前拥有的:
import requests
from bs4 import BeautifulSoup
from urllib2 import urlopen
def trade_spider(max_pages):
page = 1
while page <= max_pages:
url = 'http://www.boxofficemojo.com/yearly/chart/?page=' + str(page) + '&view=releasedate&view2=domestic&yr=2013&p=.htm'
source_code = requests.get(url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text)
for link in soup.select('td > b > font > a[href^=/movies/?]'):
href = 'http://www.boxofficemojo.com' + link.get('href')
title = link.string
print title, href
get_single_item_data(href)
def get_single_item_data(item_url):
source_code = requests.get(item_url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text)
print soup.find_all("Genre: ")
for person in soup.select('td > font > a[href^=/people/]'):
print person.string
trade_spider(1)
到目前为止,这会从原始页面检索所有电影的标题、它们的 link 以及每部电影的列表 actors/people/directors 等。现在我正在尝试获取电影的类型。
我试图以与
类似的方式来处理这个问题
"for person in soup.select('td > font > a[href^=/people/]'):
print person.string"
行,但这不是 link,它只是文本,因此无法正常工作。
如何获取每部电影的数据?
找到 Genre:
文本并得到 next sibling:
soup.find(text="Genre: ").next_sibling.text
演示:
In [1]: import requests
In [2]: from bs4 import BeautifulSoup
In [3]: response = requests.get("http://www.boxofficemojo.com/movies/?id=ironman3.htm")
In [4]: soup = BeautifulSoup(response.content)
In [5]: soup.find(text="Genre: ").next_sibling.text
Out[5]: u'Action / Adventure'
我要抓取的页面是 http://www.boxofficemojo.com/yearly/chart/?page=1&view=releasedate&view2=domestic&yr=2013&p=.htm. Specifically, I am focusing on this page right now: http://www.boxofficemojo.com/movies/?id=ironman3.htm。
对于第一部 link 上的每一部电影,我想获得类型、放映时间、MPAA 评级、国外票房收入和预算。我很难得到这个,因为信息上没有识别标签。我目前拥有的:
import requests
from bs4 import BeautifulSoup
from urllib2 import urlopen
def trade_spider(max_pages):
page = 1
while page <= max_pages:
url = 'http://www.boxofficemojo.com/yearly/chart/?page=' + str(page) + '&view=releasedate&view2=domestic&yr=2013&p=.htm'
source_code = requests.get(url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text)
for link in soup.select('td > b > font > a[href^=/movies/?]'):
href = 'http://www.boxofficemojo.com' + link.get('href')
title = link.string
print title, href
get_single_item_data(href)
def get_single_item_data(item_url):
source_code = requests.get(item_url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text)
print soup.find_all("Genre: ")
for person in soup.select('td > font > a[href^=/people/]'):
print person.string
trade_spider(1)
到目前为止,这会从原始页面检索所有电影的标题、它们的 link 以及每部电影的列表 actors/people/directors 等。现在我正在尝试获取电影的类型。
我试图以与
类似的方式来处理这个问题"for person in soup.select('td > font > a[href^=/people/]'):
print person.string"
行,但这不是 link,它只是文本,因此无法正常工作。
如何获取每部电影的数据?
找到 Genre:
文本并得到 next sibling:
soup.find(text="Genre: ").next_sibling.text
演示:
In [1]: import requests
In [2]: from bs4 import BeautifulSoup
In [3]: response = requests.get("http://www.boxofficemojo.com/movies/?id=ironman3.htm")
In [4]: soup = BeautifulSoup(response.content)
In [5]: soup.find(text="Genre: ").next_sibling.text
Out[5]: u'Action / Adventure'