第一次爬取的網站就是豆瓣電影 Top 250,網址是:https://movie.douban.com/top250?start=0&filter=
分析網址'?'符號后的參數,第一個參數'start=0',這個代表頁數,‘=0’時代表第一頁,‘=25’代表第二頁。。。以此類推
一、分析網頁:
明確要爬取的元素 :排名、名字、導演、評語、評分,在這里利用Chrome瀏覽器,查看元素的所在位置
每一部電影信息都在<li></li>當中
爬取元素的所在位置
分析完要爬取的元素,開始准備爬取的工作
二、爬取部分:
工具:
Python3
requests
BeautifulSoup
1、獲取每一部電影的信息
1 def get_html(web_url): # 爬蟲獲取網頁沒啥好說的
2 header = { 3 "User-Agent":"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 Safari/534.16"} 4 html = requests.get(url=web_url, headers=header).text#不加text返回的是response,加了返回的是字符串
5 Soup = BeautifulSoup(html, "lxml") 6 data = Soup.find("ol").find_all("li") # 還是有一點要說,就是返回的信息最好只有你需要的那部分,所以這里進行了篩選
7 return data
requests.get()函數,會根據參數中url的鏈接,返回response對象
.text會將response對象轉換成str類型
find_all()函數,會將html文本中的ol標簽下的每一個li標簽中的內容篩選出來
2、篩選出信息,保存進文本
1 def get_info(all_move): 2 f = open("F:\\Pythontest1\\douban.txt", "a") 3
4 for info in all_move: 5 # 排名
6 nums = info.find('em') 7 num = nums.get_text() 8
9 # 名字
10 names = info.find("span") # 名字比較簡單 直接獲取第一個span就是
11 name = names.get_text() 12
13 # 導演
14 charactors = info.find("p") # 這段信息中有太多非法符號你需要替換掉
15 charactor = charactors.get_text().replace(" ", "").replace("\n", "") # 使信息排列規律
16 charactor = charactor.replace("\xa0", "").replace("\xee", "").replace("\xf6", "").replace("\u0161", "").replace( 17 "\xf4", "").replace("\xfb", "").replace("\u2027", "").replace("\xe5", "") 18
19 # 評語
20 remarks = info.find_all("span", {"class": "inq"}) 21 if remarks: # 這個判斷是因為有的電影沒有評語,你需要做判斷
22 remark = remarks[0].get_text().replace("\u22ef", "") 23 else: 24 remark = "此影片沒有評價"
25 print(remarks) 26
27 # 評分
28 scores = info.find_all("span", {"class": "rating_num"}) 29 score = scores[0].get_text() 30
31
32 f.write(num + '、') 33 f.write(name + "\n") 34 f.write(charactor + "\n") 35 f.write(remark + "\n") 36 f.write(score) 37 f.write("\n\n") 38
39 f.close() # 記得關閉文件
注意爬取元素的時候,會有非法符號(因為這些符號的存在,會影響你寫入文本中),所以需要將符號用replace函數替換
其余的部分就不做解釋了~~
3、全部代碼
1 from bs4 import BeautifulSoup 2 import requests 3 import os 4
5
6 def get_html(web_url): # 爬蟲獲取網頁沒啥好說的
7 header = { 8 "User-Agent":"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 Safari/534.16"} 9 html = requests.get(url=web_url, headers=header).text#不加text返回的是response,加了返回的是字符串
10 Soup = BeautifulSoup(html, "lxml") 11 data = Soup.find("ol").find_all("li") # 還是有一點要說,就是返回的信息最好只有你需要的那部分,所以這里進行了篩選
12 return data 13
14
15 def get_info(all_move): 16 f = open("F:\\Pythontest1\\douban.txt", "a") 17
18 for info in all_move: 19 # 排名
20 nums = info.find('em') 21 num = nums.get_text() 22
23 # 名字
24 names = info.find("span") # 名字比較簡單 直接獲取第一個span就是
25 name = names.get_text() 26
27 # 導演
28 charactors = info.find("p") # 這段信息中有太多非法符號你需要替換掉
29 charactor = charactors.get_text().replace(" ", "").replace("\n", "") # 使信息排列規律
30 charactor = charactor.replace("\xa0", "").replace("\xee", "").replace("\xf6", "").replace("\u0161", "").replace( 31 "\xf4", "").replace("\xfb", "").replace("\u2027", "").replace("\xe5", "") 32
33 # 評語
34 remarks = info.find_all("span", {"class": "inq"}) 35 if remarks: # 這個判斷是因為有的電影沒有評語,你需要做判斷
36 remark = remarks[0].get_text().replace("\u22ef", "") 37 else: 38 remark = "此影片沒有評價"
39 print(remarks) 40
41 # 評分
42 scores = info.find_all("span", {"class": "rating_num"}) 43 score = scores[0].get_text() 44
45
46 f.write(num + '、') 47 f.write(name + "\n") 48 f.write(charactor + "\n") 49 f.write(remark + "\n") 50 f.write(score) 51 f.write("\n\n") 52
53 f.close() # 記得關閉文件
54
55
56 if __name__ == "__main__": 57 if os.path.exists("F:\\Pythontest1") == False: # 兩個if來判斷是否文件路徑存在 新建文件夾 刪除文件
58 os.mkdir("F:\\Pythontest1") 59 if os.path.exists("F:\\Pythontest1\\douban.txt") == True: 60 os.remove("F:\\Pythontest1\\douban.txt") 61
62 page = 0 # 初始化頁數,TOP一共有250部 每頁25部
63 while page <= 225: 64 web_url = "https://movie.douban.com/top250?start=%s&filter=" % page 65 all_move = get_html(web_url) # 返回每一頁的網頁
66 get_info(all_move) # 匹配對應信息存入本地
67 page += 25