蓄(tuo)謀(yan)已久的我終于開(kāi)始正式學(xué)習(xí)python啦,學(xué)習(xí)了三個(gè)教程:
? ? 1.python基礎(chǔ):《父與子的編程之旅》,通俗的python教程。
? ? 2.爬蟲(chóng)基礎(chǔ):Python爬蟲(chóng)學(xué)習(xí)系列教程_by崔慶才
? ? 3.方便好用的庫(kù):Beautiful Soup 4.4.0 文檔【官方】
然后成功寫(xiě)出了一只能蠕動(dòng)的爬蟲(chóng),雞凍!雞凍!雖然很渣,但是終于成功了雞凍??!
# coding:utf-8
#?爬取指定頁(yè)碼的糗事百科24h頁(yè)面的作者、內(nèi)容、點(diǎn)贊數(shù)、評(píng)論數(shù)
import?requests
from?bs4?import?BeautifulSoup
while?True?:
? ? page?=?raw_input('請(qǐng)輸入要顯示的“糗事百科24h”頁(yè)碼:?')
? ? 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?'_____________________________________'