【发布时间】:2015-07-09 23:49:13
【问题描述】:
我要抓取的网站是http://www.boxofficemojo.com/yearly/chart/?yr=2015&p=.htm。 这个网站有一个电影列表,对于每部电影,我想在表格中获取以下信息,不包括日期。
我遇到了这个问题,因为文本没有链接或任何类标签。我已经尝试过使用多种方法,但都没有奏效。
这是我目前使用的一种方法,只是为了获得每部电影的排名。 我希望输出只是由每部电影的排名组成的列表列表,然后是另一个包含每部电影列表、周末总票房等的列表。
listOfRanks = [[1, 1, 1,], [1, 2, 3], [3, 5,1]], etc.
listOfWeekendGross = [[208,806,270,106588440,54200000], [111111111, 222222222, 333333333]]
def getRank(item_url):
href = item_url[:37]+"page=weekend&" + item_url[37:]
response = requests.get(href)
soup = BeautifulSoup(response.content, "lxml") # or BeautifulSoup(response.content, "html5lib")
rank = soup.select('tbody > tr > td > center > table > tbody > tr > td > font')
print rank
这就是我调用这个函数的地方 -
def spider(max_pages):
url = 'http://www.boxofficemojo.com/yearly/chart/?page=' + str(max_pages) + '&view=releasedate&view2=domestic&yr=2015&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')
getRank(href)
问题在于 getRank(href) 方法没有将排名正确添加到列表中。我认为问题在于这条线 -
rank = soup.select('tbody > tr > td > center > table > tbody > tr > td > font')
这可能不是获取此文本的正确方法。
我如何从这个网站获得所有排名、周末总收入等?
++++++++++++++++++++++++++++++++++++
【问题讨论】:
标签: python beautifulsoup web-crawler