代碼如下:
# -*- coding:utf-8 -*-
from urllib.request import urlopen
import bs4
import webbrowser
import requests
html_list = [] #存放要爬的所有網頁
html_list_txt = [] #存放要爬的所有網頁的源代碼
movie_list = [] #存放所有電影
#movie_total = {} #存放所有電影資料的字典
#def add_movie():
html = 'https://movie.douban.com/top250'
html_list.append(html)
#html2 = requests.get('https://movie.douban.com/top250')
#webbrowser.open(html)
#webbrowser.open(html)
html_txt = (urlopen(html)).read()
#html_txt = (requests.get(html)).text
bsObj = bs4.BeautifulSoup(html_txt, 'html.parser')
print('---1---')
html_div = bsObj.find('div',{'class':'paginator'})
print('---2---')
html_a = html_div.findAll('a')
print('---3---')
for html_a_temp in html_a:
# print(type(html_a_temp))
#i = 2
#while i <= 9:
#print(html_a_temp.get_text())
#if html_a_temp.get_text != '后頁>':
html_href = html_a_temp.attrs['href']
html_ + html_href
html_list.append(html_href)
#i += 1
print('---4---')
html_list = list(set(html_list))
print(len(html_list))
#print(html_set)
#print(len(html_set))
#print(set(html_list))
#print(type('后頁'))
#上面的代碼為找到所有的鏈接
for html_list_temp in html_list:
'''把所有要爬的鏈接全部轉換成源代碼并存儲'''
html_read = bs4.BeautifulSoup(urlopen(html_list_temp).read(), 'html.parser')
html_list_txt.append(html_read)
for html_page in html_list_txt:
name_div_list = html_page.findAll('div',{'class':'info'})
for name_div_temp in name_div_list:
movie_total = {} #創(chuàng)建一個局部變量的字典,用來存放臨時找到的某個電影的資料
name_div_inside = name_div_temp.findAll('div')
movie_name = name_div_inside[0].a.span.get_text() #找到電影名字
name_div_star = name_div_temp.find('div',{'class':'star'})
name_div_star_span = name_div_star.findAll('span')
movie_score = name_div_star_span[1].get_text() #找到電影評分
movie_number = name_div_star_span[3].get_text() #找到電影評價人數(shù)
# ------分隔符-----
try:
movie_introduction = name_div_temp.find('span',{'class':'inq'}).get_text()#找到電影簡評
except AttributeError:
print("這部電影沒有簡評~~~~~~~~~~~")
print(movie_introduction)
#name_span_inq = name_div_temp.findAll('p')[1].span.get_text()
#movie_introduction = name_span_inq
#name_span_inq = name_div_temp.find('span',{'class':'inq'})
#movie_introduction = name_span_inq.get_text() #找到電影簡評
movie_total['name'] = movie_name
movie_total['score'] = movie_score
movie_total['number'] = movie_number
movie_total['introduction'] = movie_introduction
movie_list.append(movie_total)
print(movie_list)
'''
name_div_inside_span_list = name_div_inside[1].div.findAll('span')
for name_div_inside_span_temp in name_div_inside_span_list:
movie_score = name_div_inside[1].div.span[1].get_text() #找到電影評分
movie_number = name_div_inside[1].div.span[3].get_text() #找到評價人數(shù)
'''
#movie_total[name] = movie_name
'''
name_div_list = html_page.findAll('div',{'class':'hd'})
for name_div_temp in name_div_list:
movie_name = name_div_temp.a.span.get_text()
movie_name_list.append(movie_name)
'''
#print(movie_name_list)
運行結果如下:
藍色箭頭位置,拋出了異常處理,但是緊接著,前一部電影的簡評又被打印了一次。這是怎么回事呢?
北大青鳥APTECH成立于1999年。依托北京大學優(yōu)質雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
達內教育集團成立于2002年,是一家由留學海歸創(chuàng)辦的高端職業(yè)教育培訓機構,是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
北大課工場是北京大學校辦產業(yè)為響應國家深化產教融合/校企合作的政策,積極推進“中國制造2025”,實現(xiàn)中華民族偉大復興的升級產業(yè)鏈。利用北京大學優(yōu)質教育資源及背
博為峰,中國職業(yè)人才培訓領域的先行者
曾工作于聯(lián)想擔任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔任項目經理從事移動互聯(lián)網管理及研發(fā)工作,曾創(chuàng)辦藍懿科技有限責任公司從事總經理職務負責iOS教學及管理工作。
浪潮集團項目經理。精通Java與.NET 技術, 熟練的跨平臺面向對象開發(fā)經驗,技術功底深厚。 授課風格 授課風格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網頁制作和網頁游戲開發(fā)。
具有10 年的Java 企業(yè)應用開發(fā)經驗。曾經歷任德國Software AG 技術顧問,美國Dachieve 系統(tǒng)架構師,美國AngelEngineers Inc. 系統(tǒng)架構師。