- 這個小爬蟲我們用到的requests、re、lxml、json和pyMySQL在編寫代碼之前我們需要將他們導入進來.
import requests from lxml import etree import pymysql import json
- 首先先選擇一個瀏覽器頭信息,這個可有可無,當然有了更好,存在它才能讓我們的爬蟲更像是從瀏覽器進入獲取信息的。
headers = { 'Connection': 'keep-alive', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (windows NT 6.1; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/69.0.3497.100 Safari/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9',}
- 接下來就是分析數據接口了,這個可以使用瀏覽器按F12,切換到編輯者模式,從中分析出數據接口鏈接,這里我們直接給出數據接口。
url = "http://v2.sohu.com/public-api/feed?scene=CHANNEL&sceneId=10&page=1&size=10"
通過數據接口形式我們看到它有兩個參數page和size,page=1表示第一頁,size=10表示每頁十條數據。我們只需要改變page的數據就可以源源不斷的獲取數據了,當然獲取數據要節制,不要給人家服務器造成太大壓力。
- 接下來直接請求網頁鏈接獲取鏈接返回的數據。這里返回的是json字符串,我們需要對json字符串進行解析,這個時候json這個庫就用到了。
json_str = requests.get(url,headers= headers).text
[{ "id": 357806424, "authorId": 557006, "authorName": "FX168", "authorPic": "//sucimg.itc.cn/avatarimg/34ca41ae9ad04be68072f8894d7124b7_1491542550231", "focus": "//5b0988e595225.cdn.sohucs.com/c_fill,w_600,h_300,g_faces/images/20191202/cc5eda06fba94b3fb7ed0c1c2faea9a6.jpeg", "picUrl": "//5b0988e595225.cdn.sohucs.com/c_fill,w_150,h_100,g_faces,q_70/images/20191202/cc5eda06fba94b3fb7ed0c1c2faea9a6.jpeg", "images": ["//5b0988e595225.cdn.sohucs.com/c_fill,w_150,h_100,g_faces,q_70/images/20191202/cc5eda06fba94b3fb7ed0c1c2faea9a6.jpeg", "//5b0988e595225.cdn.sohucs.com/c_fill,w_150,h_100,g_faces,q_70/images/20191202/df1ed938d9614cf690f87a58577ce07a.png"], "title": "?70年來首次,美國成石油凈出口國!國際油價暴跌近5%,一切才剛剛開始?", "mobileTitle": "?70年來首次,美國成石油凈出口國!國際油價暴跌近5%,一切才剛剛開始?", "tags": [{ "id": 70694, "name": "沙特", "channelId": 0, "channelName": null, "categoryId": 0, "categoryName": null, "config": null, "introduction": null, "secureScore": 100, "hotSpot": false }, { "id": 68937, "name": "美國", "channelId": 0, "channelName": null, "categoryId": 0, "categoryName": null, "config": null, "introduction": null, "secureScore": 100, "hotSpot": false }, { "id": 68938, "name": "俄羅斯", "channelId": 0, "channelName": null, "categoryId": 0, "categoryName": null, "config": null, "introduction": null, "secureScore": 100, "hotSpot": false }], "publicTime": 1575262702000, "channelId": 0, "channelName": null, "channelUrl": "", "categoryId": 0, "categoryName": null, "headImage": null, "cmsId": 0, "originalSource": "http://mp.weixin.qq.com/s?__biz=MjM5OTAwOTMyMA==&mid=2650280772&idx=1&sn=85dd7f58ab6b292fcff2d57a677a35dc", "outerLink": false, "otherId": 0, "passport": "[email protected]", "personalPage": "http://mp.sohu.com/profile?xpt=ZngxNjhjYWlqaW5nQHNvaHUuY29t", "videoInfo": null, "type": 0, "cover": null, "tkd": null, "secureScore": 100 }]
- 通過這個數據我們來提取我們自己需要的數據,比如標題,發布時間,當然如果想獲取新聞正文,還需要進入到網址鏈接中取,在數據中我們看到網址鏈接在picUrl這個字段中。搜狐的新聞鏈接是通過id與authorId拼接而成,針對上面的一條數據,這個新聞的正文鏈接便是
http://www.sohu.com/a/357806424_557006
- 通過之前的方式,我們再去請求這個網址獲取新聞正文的html原頁面,再將原頁面通過lxml加工成元素結構,便可以直接獲取數據啦!
html = requests.get(url,headers= headers).text #獲取內容 etree.HTML(str(html)).xpath(“”//article[@class = 'article']//p//text()')
- 獲取數據之后將其組裝成sql語句,以便直接插入到數據庫中。
sql = " sql_insert = 'insert into information (`type`, url,author,title,content,postTime,addtime,`unique`) values (%s,%s,%s,%s,%s,%s,%s,%s)' "
- 通過pymysql獲取mysql的連接信息執行sql語句
conn = pymysql.connect(host=“xxxxxxx”, port=3306, user="xxxx", passwd="xxxxxx", db="news", charset="utf8") cursor = conn.cursor() cursor.execute(sql_insert)
完整代碼請轉我的csdn連接!