Python爬取三國演義的實(shí)現(xiàn)方法
本文的爬蟲教程分為四部:
1.從哪爬 where
2.爬什么 what
3.怎么爬 how
4.爬了之后信息如何保存 save
一、從哪爬
二、爬什么
三國演義全文
三、怎么爬
在Chrome頁面打開F12,就可以發(fā)現(xiàn)文章內(nèi)容在節(jié)點(diǎn)
<div id="con" class="bookyuanjiao">
只要找到這個(gè)節(jié)點(diǎn),然后把內(nèi)容寫入到一個(gè)html文件即可。
content = soup.find("div", {"class": "bookyuanjiao", "id": "con"})
四、爬了之后如何保存
主要就是拿到內(nèi)容,拼接到一個(gè)html文件,然后保存下來就可以了。
#!usr/bin/env # -*-coding:utf-8 -*- import urllib2 import os from bs4 import BeautifulSoup as BS import locale import sys from lxml import etree import re reload(sys) sys.setdefaultencoding('gbk') sub_folder = os.path.join(os.getcwd(), "sanguoyanyi") if not os.path.exists(sub_folder): os.mkdir(sub_folder) path = sub_folder # customize html as head of the articles input = open(r'0.html', 'r') head = input.read() domain = 'http://www.shicimingju.com/book/sanguoyanyi.html' t = domain.find(r'.html') new_domain = '/'.join(domain.split("/")[:-2]) first_chapter_url = domain[:t] + "/" + str(1) + '.html' print first_chapter_url # Get url if chapter lists req = urllib2.Request(url=domain) resp = urllib2.urlopen(req) html = resp.read() soup = BS(html, 'lxml') chapter_list = soup.find("div", {"class": "bookyuanjiao", "id": "mulu"}) sel = etree.HTML(str(chapter_list)) result = sel.xpath('//li/a/@href') for each_link in result: each_chapter_link = new_domain + "/" + each_link print each_chapter_link req = urllib2.Request(url=each_chapter_link) resp = urllib2.urlopen(req) html = resp.read() soup = BS(html, 'lxml') content = soup.find("div", {"class": "bookyuanjiao", "id": "con"}) title = soup.title.text title = title.split(u'_《三國演義》_詩詞名句網(wǎng)')[0] html = str(content) html = head + html + "</body></html>" filename = path + "\\" + title + ".html" print filename # write file output = open(filename, 'w') output.write(html) output.close()
0.html的內(nèi)容如下
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body>
總結(jié)
以上就是利用Python爬取三國演義的實(shí)現(xiàn)方法,希望對(duì)大家學(xué)習(xí)python能有所幫助,如果有疑問大家可以留言交流。
- python動(dòng)態(tài)網(wǎng)頁批量爬取
- python爬取網(wǎng)站數(shù)據(jù)保存使用的方法
- Python實(shí)現(xiàn)爬取知乎神回復(fù)簡單爬蟲代碼分享
- 通過抓取淘寶評(píng)論為例講解Python爬取ajax動(dòng)態(tài)生成的數(shù)據(jù)(經(jīng)典)
- Python爬取Coursera課程資源的詳細(xì)過程
- python爬蟲教程之爬取百度貼吧并下載的示例
- Python使用Scrapy爬取妹子圖
- python 爬取微信文章
- python實(shí)現(xiàn)爬取千萬淘寶商品的方法
- 以視頻爬取實(shí)例講解Python爬蟲神器Beautiful Soup用法
相關(guān)文章
python人工智能自定義求導(dǎo)tf_diffs詳解
這篇文章主要為大家介紹了python人工智能自定義求導(dǎo)tf_diffs詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07Python要如何實(shí)現(xiàn)列表排序的幾種方法
這篇文章主要介紹了Python要如何實(shí)現(xiàn)列表排序的幾種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02Python中Matplotlib的點(diǎn)、線形狀、顏色以及繪制散點(diǎn)圖
與線型圖類似的是,散點(diǎn)圖也是一個(gè)個(gè)點(diǎn)集構(gòu)成的,下面這篇文章主要給大家介紹了關(guān)于Python中Matplotlib的點(diǎn)、線形狀、顏色以及繪制散點(diǎn)圖的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04python實(shí)現(xiàn)控制臺(tái)輸出顏色
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)控制臺(tái)輸出顏色,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03python下函數(shù)參數(shù)的傳遞(參數(shù)帶星號(hào)的說明)
python中函數(shù)參數(shù)的傳遞是通過賦值來傳遞的。2010-09-09詳解Python3 定義一個(gè)跨越多行的字符串的多種方法
這篇文章主要介紹了詳解Python3 定義一個(gè)跨越多行的字符串的多種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09如何利用pandas工具輸出每行的索引值、及其對(duì)應(yīng)的行數(shù)據(jù)
這篇文章主要介紹了如何利用pandas工具輸出每行的索引值、及其對(duì)應(yīng)的行數(shù)據(jù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03