Python 爬虫-抓取小说《鬼吹灯之精绝古城》

想看小说《鬼吹灯之精绝古城》,可是网页版的好多广告,还要一页一页的翻,还无法复制,于是写了个小爬虫,保存到word里慢慢看。

代码如下:

"""
爬取《鬼吹灯之精绝古城》小说
"""
from selenium import webdriver
import os
from docx import Document

class DownloadFiles():

    def __init__(self):
        self.baseUrl = ‘http://www.luoxia.com/guichui/‘
        self.basePath = os.path.dirname(__file__)

    def makedir(self, name):
        path = os.path.join(self.basePath, name)
        isExist = os.path.exists(path)
        if not isExist:
            os.makedirs(path)
            print(‘File has been created.‘)
        else:
            print(‘The file is existed.‘)
        # 切换到该目录下
        os.chdir(path)

    def connect(self, url):
        try:
            driver = webdriver.PhantomJS()
            driver.get(url)
            print(url)
        except:
            "This page is not existed."
        return driver

    def getContent(self):
        doc = Document()
        self.makedir(‘storyFiles‘)
        for page in range(27426, 27461):
            print(‘The page number is : ‘ + str(page))
            url = self.baseUrl + str(page) + ‘.htm‘
            driver = self.connect(url)
            rList = driver.find_elements_by_xpath(‘//article/p‘)
            for r in rList:
                print(r.text)
                doc.add_paragraph(r.text)

        doc.save(‘guichuideng.doc‘)

if __name__ == ‘__main__‘:
    obj = DownloadFiles()
    obj.getContent()

时间: 2024-08-06 03:32:49

Python 爬虫-抓取小说《鬼吹灯之精绝古城》的相关文章

python 爬虫抓取心得

quanwei9958 转自 python 爬虫抓取心得分享 urllib.quote('要编码的字符串') 如果你要在url请求里面放入中文,对相应的中文进行编码的话,可以用: urllib.quote('要编码的字符串') query = urllib.quote(singername) url = 'http://music.baidu.com/search?key='+query response = urllib.urlopen(url) text = response.read()

Python爬虫抓取网页图片

本文通过python 来实现这样一个简单的爬虫功能,把我们想要的图片爬取到本地. 下面就看看如何使用python来实现这样一个功能. # -*- coding: utf-8 -*- import urllib import re import time import os #显示下载进度 def schedule(a,b,c): ''''' a:已经下载的数据块 b:数据块的大小 c:远程文件的大小 ''' per = 100.0 * a * b / c if per > 100 : per =

python爬虫抓取站长之家IP库,仅供练习用!

python爬虫抓取站长之家IP库,单线程的,仅供练习,IP库数据有43亿条,如果按此种方法抓取至少得数年,所以谨以此作为练手,新手代码很糙,请大家见谅. #!/usr/bin/python #coding=UTF-8 import urllib2 import re import os import csv import codecs user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' headers = { 'User-

python 爬虫抓取心得分享

/** author: insun title:python 爬虫抓取心得分享 blog:http://yxmhero1989.blog.163.com/blog/static/112157956201311821444664/ **/    0x1.urllib.quote('要编码的字符串') 如果你要在url请求里面放入中文,对相应的中文进行编码的话,可以用: urllib.quote('要编码的字符串') query = urllib.quote(singername) url = 'h

python爬虫抓取哈尔滨天气信息

python 爬虫 爬取哈尔滨天气信息 - http://www.weather.com.cn/weather/101050101.shtml 环境: windows7 python3.4(pip install requests:pip install BeautifulSoup4) 代码:(亲测可以正确执行) 1 # coding:utf-8 2 """ 3 总结一下,从网页上抓取内容大致分3步: 4 1.模拟浏览器访问,获取html源代码 5 2.通过正则匹配,获取指定

Python爬虫抓取技术的门道

web是一个开放的平台,这也奠定了web从90年代初诞生直至今日将近30年来蓬勃的发展.然而,正所谓成也萧何败也萧何,开放的特性.搜索引擎以及简单易学的html.css技术使得web成为了互联网领域里最为流行和成熟的信息传播媒介:但如今作为商业化软件,web这个平台上的内容信息的版权却毫无保证,因为相比软件客户端而言,你的网页中的内容可以被很低成本.很低的技术门槛实现出的一些抓取程序获取到,这也就是这一系列文章将要探讨的话题-- 网络爬虫 . 有很多人认为web应当始终遵循开放的精神,呈现在页面

python爬虫——爬取小说 | 探索白子画和花千骨的爱恨情仇(转载)

转载出处:药少敏   ,感谢原作者清晰的讲解思路! 下述代码是我通过自己互联网搜索和拜读完此篇文章之后写出的具有同样效果的爬虫代码: 1 from bs4 import BeautifulSoup 2 import requests 3 4 if __name__ == '__main__': 5 html = requests.get('http://www.136book.com/huaqiangu/') 6 soup = BeautifulSoup(html.content, 'lxml'

Python爬虫抓取 python tutorial中文版,保存为word

看到了中文版的python tutorial,发现是网页版的,刚好最近在学习爬虫,想着不如抓取到本地 首先是网页的内容 查看网页源码后发现可以使用BeautifulSoup来获取文档的标题和内容,并保存为doc文件. 这里需要使用from bs4 import BeautifulSoup 来导入该模块 具体代码如下: # 输出所在网址的内容from bs4 import BeautifulSoup def introduce(url): res = requests.get(url) res.e

Python爬虫--抓取糗事百科段子

今天使用python爬虫实现了自动抓取糗事百科的段子,因为糗事百科不需要登录,抓取比较简单.程序每按一次回车输出一条段子,代码参考了 http://cuiqingcai.com/990.html 但该博主的代码似乎有些问题,我自己做了修改,运行成功,下面是代码内容: 1 # -*- coding:utf-8 -*- 2 __author__ = 'Jz' 3 import urllib2 4 import re 5 6 #糗事百科爬虫类 7 class QSBK: 8 #初始化 9 def __