0.工具
先說一下python版本辐烂,我用的是python3.6扫倡、requests和beautifulsoup,版本不同可能會出現(xiàn)問題邻寿,現(xiàn)在都已經(jīng)2017年了污茵,建議大家Python可以用最新的了
1.爬取結果
"D:\Program Files (x86)\Python\python.exe" E:/Python/Test.py
{'url': 'http://www.cnbeta.com/articles/tech/665159.htm', 'title': '網(wǎng)友新買氣壓椅突然爆炸:電腦機箱被炸裂'}
{'url': 'http://www.cnbeta.com/articles/soft/665157.htm', 'title': 'Android QQ輕聊版 3.6.1 正式版發(fā)布'}
{'url': 'http://www.cnbeta.com/articles/soft/665155.htm', 'title': '微信 2.6 for Windows正式版發(fā)布 可以搜索本地聊天記錄'}
{'url': 'http://www.cnbeta.com/articles/tech/665153.htm', 'title': '中國獨立制造的第六代深水鉆井平臺順利完成海試'}
{'url': 'http://www.cnbeta.com/articles/tech/665151.htm', 'title': '楊利偉榮獲聯(lián)合國教科文組織“空間科學獎章”'}
{'url': 'http://www.cnbeta.com/articles/tech/665149.htm', 'title': '突破性治療幫助癱瘓的男子再次移動雙腿'}
{'url': 'http://www.cnbeta.com/articles/tech/665147.htm', 'title': '不需高端設備:普通燈泡也能實現(xiàn)觸摸投影'}
{'url': 'http://www.cnbeta.com/articles/tech/665145.htm', 'title': '廣州共享汽車征求意見:用戶年滿18周歲并有一年駕駛經(jīng)歷'}
{'url': 'http://hot.cnbeta.com/articles/funny/665143.htm', 'title': '河南大爺推翻多輛共享單車:這樣更安全'}
{'url': 'http://hot.cnbeta.com/articles/movie/665141.htm', 'title': '《銀翼殺手2049》內地票房遇冷 觀眾口碑兩極分化'}
{'url': 'http://www.cnbeta.com/articles/tech/665139.htm', 'title': '高通官方預告驍龍845:12月毛伊島見'}
{'url': 'http://www.cnbeta.com/articles/tech/665137.htm', 'title': '中國移動砸8400萬復活飛信?先活下來再說'}
{'url': 'http://www.cnbeta.com/articles/tech/665135.htm', 'title': '北歐銀行擬裁員六千 將用智能機器替代'}
{'url': 'http://hot.cnbeta.com/articles/game/665133.htm', 'title': '廣電巨石砸向“吃雞” 剛剛入局的小米該怎么辦?'}
{'url': 'http://www.cnbeta.com/articles/tech/665131.htm', 'title': '科學家:銀河系或有大量外星人可能藏身冰下黑暗海洋'}
{'url': 'http://www.cnbeta.com/articles/tech/665129.htm', 'title': '中國又一巨頭獲重大突破:能卷起來的手機走來'}
{'url': 'http://www.cnbeta.com/articles/tech/665127.htm', 'title': '蘋果USB-C to Lightning快充數(shù)據(jù)線破解成功'}
{'url': 'http://www.cnbeta.com/articles/tech/665125.htm', 'title': '通用電氣考慮出售旗下火車制造部門'}
{'url': 'http://www.cnbeta.com/articles/tech/665123.htm', 'title': '日制造業(yè)丑聞不斷:神鋼添新問題 斯巴魯陷檢驗門'}
{'url': 'http://www.cnbeta.com/articles/tech/665121.htm', 'title': '劉強東透露兒時最大夢想是:能吃上肉'}
{'url': 'http://www.cnbeta.com/articles/tech/665119.htm', 'title': '三大運營商財報:聯(lián)通4G用戶數(shù)有望年底超過電信'}
{'url': 'http://www.cnbeta.com/articles/tech/665117.htm', 'title': '特斯拉股價接近熊市區(qū)域 未來可能下跌更多'}
{'url': 'http://www.cnbeta.com/articles/tech/665115.htm', 'title': '路透:全球自動駕駛行業(yè)初創(chuàng)企業(yè)多達240余家 存在泡沫'}
{'url': 'http://www.cnbeta.com/articles/tech/665113.htm', 'title': '應用適配不到位 首批蘋果iPhone X用戶可能要痛苦一陣'}
{'url': 'http://www.cnbeta.com/articles/tech/665111.htm', 'title': '[圖]索尼概念汽車即將上路蝙昙?SC-1正研發(fā)中'}
{'url': 'http://www.cnbeta.com/articles/tech/665109.htm', 'title': 'Moto青柚上手評測:定位千元的摩托羅拉你考慮嗎闪萄?'}
{'url': 'http://www.cnbeta.com/articles/tech/665107.htm', 'title': '[視頻]建個房屋等一年?交給這個公司10分鐘就辦好'}
{'url': 'http://hot.cnbeta.com/articles/game/665105.htm', 'title': '柯南秀節(jié)目現(xiàn)場玩《刺客信條:起源》 結果主角飛了起來'}
{'url': 'http://www.cnbeta.com/articles/tech/665103.htm', 'title': '劉強東攜章澤天參加母校宿遷中學90年校慶 春風滿面'}
{'url': 'http://www.cnbeta.com/articles/tech/665101.htm', 'title': '學校會計挪用公款1600萬 用1300萬打賞女主播'}
{'url': 'http://www.cnbeta.com/articles/science/665099.htm', 'title': '超靈敏的秤能夠稱出單個細胞的重量'}
{'url': 'http://www.cnbeta.com/articles/tech/665097.htm', 'title': '虛幻引擎4.18(Unreal Engine)正式發(fā)布 支持macOS High Sierra以及ARKit'}
{'url': 'http://www.cnbeta.com/articles/science/665095.htm', 'title': '研究發(fā)現(xiàn)碳水化合物的“味道”可能使我們變胖'}
{'url': 'http://www.cnbeta.com/articles/tech/665093.htm', 'title': 'CAT堅固型手機S41開始接受預訂 零售價約合3482.6元'}
{'url': 'http://hot.cnbeta.com/articles/game/665091.htm', 'title': '《最終幻想15》PC配置需求曝光:老爺機也能玩'}
Process finished with exit code 0
2.代碼
利益于python的簡潔奇颠,十多行代碼就把cnbeta給解析出來了败去,要是java那可就啰嗦了
# coding:utf-8
# 引入相關模塊
import requests
from bs4 import BeautifulSoup
url = "http://www.cnbeta.com/"
data = requests.get(url).content
data = str(data,"utf-8")
soup = BeautifulSoup(data,'lxml')
list = soup.select("dl > dt > a")
for i in list:
url = i.get("href")
title = i.text
out ={
'url':url,
'title':title
}
print(out)
注意
用requests拿回來的html是亂碼,這里轉成utf-8大刊,每個網(wǎng)站編碼不同,這里不是必須的三椿,
soup = BeautifulSoup(data,'lxml')這一行要裝“l(fā)xml”庫用來解析速度更快缺菌,用pip install lxml安裝就好了,
list = soup.select("dl > dt > a")的"dl > dt > a"對應著dl搜锰、dt伴郁、a標簽
最后
大家周末愉快,話說今天去圖書館沒有帶錢辦不借書證蛋叼,連門都不讓進焊傅。
最后帶上BeautifulSoup api 文檔https://www.crummy.com/software/BeautifulSoup/bs4/doc/