Python制作爬虫采集小说

yipeiwu_com7年前Python爬虫

开发工具:python3.4
操作系统:win8
主要功能:去指定小说网页爬小说目录,按章节保存到本地,并将爬过的网页保存到本地配置文件。
被爬网站:http://www.cishuge.com/
小说名称:灵棺夜行
代码出处:本人亲自码的

import urllib.request
import http.cookiejar

import socket
import time
import re

timeout = 20
socket.setdefaulttimeout(timeout)

sleep_download_time = 10
time.sleep(sleep_download_time)
 
def makeMyOpener(head = {
 'Connection': 'Keep-Alive',
 'Accept': 'text/html, application/xhtml+xml, */*',
 'Accept-Language': 'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3',
 'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko'
}):
 cj = http.cookiejar.CookieJar()
 opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
 header = []
 for key, value in head.items():
  elem = (key, value)
  header.append(elem)
 opener.addheaders = header
 return opener
 
def saveFile(save_path,txts):
 f_obj = open(save_path,'w+')
 for item in txts:
  f_obj.write(item+'\n')
 f_obj.close()
 
#get_code_list
code_list='http://www.cishuge.com/read/0/771/'
oper = makeMyOpener()
uop = oper.open(code_list,timeout=1000)
data = uop.read().decode('gbk','ignore')

pattern = re.compile('<li><a href="(.*?)".*?>(.*?)</a></li>',re.S)

items = re.findall(pattern,data)

print ('获取列表完成')
url_path='url_file.txt'

url_r=open(url_path,'r')
url_arr=url_r.readlines(100000)
url_r.close()
print (len(url_arr))

url_file=open(url_path,'a')

print ('获取已下载网址')

for tmp in items:
 save_path = tmp[1].replace(' ','')+'.txt'
 url = code_list+tmp[0]
 if url+'\n' in url_arr:
  continue
 print('写日志:'+url+'\n')
 url_file.write(url+'\n')
 opene = makeMyOpener()
 op1 = opene.open(url,timeout=1000)
 data = op1.read().decode('gbk','ignore')
 opene.close()
 pattern = re.compile('    (.*?)<br />',re.S)
 txts = re.findall(pattern,data)
 saveFile(save_path,txts)
 
url_file.close()

虽然代码还是有点瑕疵,还是分享给大家,一起改进

相关文章

python3之微信文章爬虫实例讲解

前提: python3.4 windows 作用:通过搜狗的微信搜索接口http://weixin.sogou.com/来搜索相关微信文章,并将标题及相关链接导入Excel表格中 说明:...

Python抓取聚划算商品分析页面获取商品信息并以XML格式保存到本地

本文实例为大家分享了Python抓取聚划算商品页面获取商品信息并保存的具体代码,供大家参考,具体内容如下 #!/user/bin/python # -*- coding: gbk...

Python 抓取微信公众号账号信息的方法

Python 抓取微信公众号账号信息的方法

搜狗微信搜索提供两种类型的关键词搜索,一种是搜索公众号文章内容,另一种是直接搜索微信公众号。通过微信公众号搜索可以获取公众号的基本信息及最近发布的10条文章,今天来抓取一下微信公众号的...

Python 爬虫模拟登陆知乎

Python 爬虫模拟登陆知乎

在之前写过一篇使用python爬虫爬取电影天堂资源的文章,重点是如何解析页面和提高爬虫的效率。由于电影天堂上的资源获取权限是所有人都一样的,所以不需要进行登录验证操作,写完那篇文章后又花...

Python使用Scrapy爬取妹子图

Python Scrapy爬虫,听说妹子图挺火,我整站爬取了,上周一共搞了大概8000多张图片。和大家分享一下。 核心爬虫代码 # -*- coding: utf-8 -*- fro...