天天看點

了解爬蟲原理

 作業來源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2881

1. 簡單說明爬蟲原理

網際網路就像一張大的蜘蛛網,資料便是存放在蜘蛛網的各個節點,爬蟲就像一隻蜘蛛,沿着網絡抓去自己需要的資料。爬蟲:向網站發起請求,擷取資源後進行分析并提取有用的資料的程式

2. 了解爬蟲開發過程

1).簡要說明浏覽器工作原理;

使用者輸入網址,浏覽器發送到伺服器,浏覽器接收到傳回的資料後,會解析其内容來顯示給使用者。

2).使用 requests 庫抓取網站資料;

requests.get(url) 擷取校園新聞首頁html代碼

3).了解網頁

寫一個簡單的html檔案,包含多個标簽,類,id

簡單的網頁:

1 <!DOCTYPE html>
 2 <html>
 3 <head> 
 4 <meta charset="utf-8"> 
 5 <title>菜鳥教程(runoob.com)</title> 
 6 </head>
 7 <body>
 8 
 9 <table width="500" border="0">
10 <tr>
11 <td colspan="2" style="background-color:#FFA500;">
12 <h1>主要的網頁标題</h1>
13 </td>
14 </tr>
15 
16 <tr>
17 <td style="background-color:#FFD700;width:100px;vertical-align:top;">
18 <b>菜單</b><br>
19 HTML<br>
20 CSS<br>
21 JavaScript
22 </td>
23 <td style="background-color:#eeeeee;height:200px;width:400px;vertical-align:top;">
24 内容在這裡</td>
25 </tr>
26 
27 <tr>
28 <td colspan="2" style="background-color:#FFA500;text-align:center;">
29 版權 © runoob.com</td>
30 </tr>
31 </table>
32 
33 </body>
34 </html>      

4).使用 Beautiful Soup 解析網頁;

通過BeautifulSoup(html_sample,'html.parser')把上述html檔案解析成DOM Tree

select(選擇器)定位資料

找出含有特定标簽的html元素

找出含有特定類名的html元素

找出含有特定id名的html元素

import requests
from bs4 import BeautifulSoup

url = "http://news.gzcc.cn/html/2019/jxky_0329/11094.html"
res = requests.get(url)
print(res.encoding)
res.encoding = 'utf-8'

soup = BeautifulSoup(res.text,'html.parser')

#擷取h4标簽元素
h = soup.select('h4')
print(h)

# 擷取類為shou-title的标簽元素
class_tag = soup.select(".show-title")
print(class_tag)

# 擷取id為hits的标簽元素
click_count = soup.select('#hits')
print(click_count)      

效果圖如下:

了解爬蟲原理

3.提取一篇校園新聞的标題、釋出時間、釋出機關

url = 'http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0320/11029.html'

選取的網址為http://news.gzcc.cn/html/2019/jxky_0329/11094.html

1 from urllib import request
 2 import re
 3 import datetime
 4 import time
 5 import requests
 6 from bs4 import BeautifulSoup
 7 
 8 url = "http://news.gzcc.cn/html/2019/jxky_0329/11094.html"
 9 res = requests.get(url)
10 print(res.encoding)
11 res.encoding = 'utf-8'
12 
13 soup = BeautifulSoup(res.text,'html.parser')
14 
15 info = soup.select('.show-info')[0].text
16 info = info.split("\xa0\xa0")
17 dict = {"作者":'',"釋出時間":'',"來源":'',"點選":0}
18 for i in info:
19     if(':'in i):
20         temp = i.split("釋出時間:")
21         dict["釋出時間"]=temp[1]
22         # print(temp)
23         temp={}
24     if ( ':' in i):
25         temp = i.split(":")
26         dict[temp[0]] = temp[1]
27         # print(temp)
28         temp = {}
29 
30 # 擷取點選次數
31 url2 = "http://oa.gzcc.cn/api.php?op=count&id=11094&modelid=80"
32 res2 = requests.get(url2)
33 # print(res2.text)
34 # print (re.findall(r"\$\('#hits'\).html\('(\d+)",res2.text))
35 dict['點選']=int(re.findall(r"\$\('#hits'\).html\('(\d+)",res2.text)[0])
36 
37 # 時間轉換
38 dict["釋出時間"] = datetime.datetime.strptime(dict["釋出時間"],"%Y-%m-%d %H:%M:%S ")
39 print("釋出時間類型為",type(dict["釋出時間"]))
40 # 擷取标題
41 title = soup.select(".show-title")[0].text
42 
43 # 擷取内容
44 content = soup.select(".show-content")[0].text
45 
46 print("文章标題為",title)
47 print("作者為",dict["作者"])
48 print("釋出時間為",dict["釋出時間"])
49 print("點選次數為",dict["點選"])
50 print("釋出機關為",dict["來源"])
51 print("内容為",content)      

運作效果圖:

了解爬蟲原理