python爬取糗百第一頁的笑話,python第一頁笑話

來源:互聯網
上載者:User

python爬取糗百第一頁的笑話,python第一頁笑話

自學python網路爬蟲,發現request比urllib還是要好用一些,因此利用request和BeautifulSoup來實現糗百的首頁笑話的抓取。
BeautifulSoup通過find和findAll以及利用Regex實現HTML對應模組的抓取,當然select也是一個不錯的選擇。
下面是臨時的代碼,後續還會繼續完善
 1 # coding=utf-8 2 import requests 3 from bs4 import BeautifulSoup 4  5 page = 1 6 url = 'http://www.qiushibaike.com/hot/page/' + str(page) 7 try: 8     res=requests.get(url) 9     # print res.text # 如果請求成功,下載的網頁就作為一個字串,儲存在相應的text變數中,這就是為什麼用res.text。10 except Exception as e:11     print '開啟網頁出現異常:',e12 13 try:14     soup=BeautifulSoup(res.text,'html.parser')15     elms=soup.select('.content') #這裡產生一個列表16     for elm in elms:17         print elm.text18 except Exception as e:19     print '解析出現異常:',e

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.