我的第一個爬蟲——爬取糗事百科

蓄(tuo)謀(yan)已久的我終于開始正式學習python啦,學習了三個教程:

? ? 1.python基礎:《父與子的編程之旅》,通俗的python教程。

? ? 2.爬蟲基礎:Python爬蟲學習系列教程_by崔慶才

? ? 3.方便好用的庫:Beautiful Soup 4.4.0 文檔【官方】

然后成功寫出了一只能蠕動的爬蟲,雞凍!雞凍!雖然很渣,但是終于成功了雞凍??!

# coding:utf-8

#?爬取指定頁碼的糗事百科24h頁面的作者、內容、點贊數、評論數

import?requests

from?bs4?import?BeautifulSoup


while?True?:

? ? page?=?raw_input('請輸入要顯示的“糗事百科24h”頁碼:?')

? ? url?=?'http://www.qiushibaike.com/hot/page/'?+?page

? ? user_agent?=?'Mozilla/4.0?(compatible;?MSIE?5.5;?Windows?NT)'

? ? headers?=?{'User-Agent':?user_agent}

? ? html?=?requests.get(url,?headers?=?headers)


? ? soup?=?BeautifulSoup(html.text,?'lxml')

? ? content_left?=?soup.find('div',?id?=?'content-left',?class_?=?'col1')

? ? authors?=?content_left.find_all('h2')

? ? contents?=?content_left.find_all('div',?class_?=?'content')

? ? comments?=?content_left.find_all('span',?class_?=?'stats-comments')

? ? votes?=?content_left.find_all('span',?class_?=?'stats-vote')


? ? for?i?in?range(int(len(contents))):

? ? ? ? print?authors[i].text

? ? ? ? print?contents[i].text

? ? ? ? print?votes[i].text

? ? ? ? print?comments[i].text

? ? ? ? print?'_____________________________________'

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容