from urllib import request from bs4 import BeautifulSoup as bs #爬取豆瓣最受關注圖書榜 resp = request.urlopen('https://book.douban.com/chart?subcat=I') html_data = resp.read().decode('utf-8') #轉化為BeautifulSoup對象 soup = bs(html_data,'html.parser') #搜索最受關注的圖書列表 topchart_book =soup.find_all('ul',class_='chart-dashed-list') #搜索列表中所有圖書 topchart_book_list = topchart_book[0].find_all('li',class_='media clearfix') #新建數組用於存放后續的數據 topchart_list = [] #遍歷圖書館列表,從中過濾出我們所需的信息 for item in topchart_book_list: #新建字典用於存放我們的圖書信息,之后可用class來存儲 topchart_dict = {} #搜索到具體信息的位置 book_item = item.find('a',class_='fleft') book_items = item.find('span',class_='font-small color-red fleft') #得到圖書ID topchart_dict['id'] = book_item['href'].split('/')[4] #得到圖書名稱 topchart_dict['name'] = book_item.getText().replace('\t','').replace('\n','').replace(' ','') #圖書名字 #得到圖書評分 topchart_dict['grade'] = book_items.getText() #將圖書信息加入到數組中 topchart_list.append(topchart_dict) print(topchart_list)