# - *- coding:utf-8-*-
import urllib2
import re
import os
import threading
import sys
reload(sys)
sys.setdefaultencoding('utf-8') #编码
from bs4 import BeautifulSoup
os.mkdir(u'小说0')
os.chdir(u'小说0')
def get_url():
User_Agent= 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0'
url="http://f.qidian.com/all?size=-1&sign=-1&tag=-1&chanId=-1&subCateId=-1&orderId=&update=-1&page=1&month=-1&style=1&action=1"
headers={'User-Agent':User_Agent}
request=urllib2.Request(url,headers=headers)
html=urllib2.urlopen(request).read()
soup = BeautifulSoup(html, 'html.parser')
l = soup.find_all('div', class_ = 'book-mid-info')
print # for htmltile in l:
name = htmltile.find('h4').encode('utf-8')
reg=r'<h4><a data-bid=".*?" data-eid=".*?" href="(.*?)" target="_blank">(.*?)</a></h4>'
text=re.findall(reg,name) return text
def get_content(curl,title):
os.mkdir(title.encode('gbk')) #创建目录
#os.chdir(title.encode('gbk')) #在当前目录下操作
html1 = urllib2.urlopen('http:'+curl+'#Catalog').read()
reg=re.compile(r'<li data-rid=".*?"><a href="(.*?)" target="_blank" data-eid="qd_G55" data-cid=".*?" title=".*?">(.*?)</a>')
titles=re.finditer(reg,html1) for n in titles:
curl_=n.group(1)
names=n.group(2) fd=open(title.encode('gbk')+'/'+names.encode('gbk')+'.txt','wb') #在指定目录下创建文件
#fd=open(names.encode('gbk')+'.txt','wb')
print "正在爬取%s本"%names
htmlll=urllib2.urlopen('http:'+curl_).read()
regs=re.compile(r'<div class="read-content j_readContent">\s*([\s\S]*?)\s*</div>') #正则多行时注意用\s*
content=re.findall(regs,htmlll)
for m in content:
contents=m.replace('<p>','\r\n')
fd.write(names+'\r\n'+contents)
print "已完成%s"%names
fd.close() threads=[]
def main():
for i in get_url():
th=threading.Thread(target= get_content,args=(i[0],i[1]))
threads.append(th)
for t in threads:
t.start()
while True:
if len(threading.enumerate())<10:#控制线程数量
break
if __name__=='__main__':
main()

多线程爬取 threading.Thread 文件名支持gbk编码的更多相关文章

  1. Python3 多线程爬取梨视频

    多线程爬取梨视频 from threading import Thread import requests import re # 访问链接 def access_page(url): respons ...

  2. Python爬虫入门教程 14-100 All IT eBooks多线程爬取

    All IT eBooks多线程爬取-写在前面 对一个爬虫爱好者来说,或多或少都有这么一点点的收集癖 ~ 发现好的图片,发现好的书籍,发现各种能存放在电脑上的东西,都喜欢把它批量的爬取下来. 然后放着 ...

  3. 实现多线程爬取数据并保存到mongodb

    多线程爬取二手房网页并将数据保存到mongodb的代码: import pymongo import threading import time from lxml import etree impo ...

  4. Python爬虫入门教程: All IT eBooks多线程爬取

    All IT eBooks多线程爬取-写在前面 对一个爬虫爱好者来说,或多或少都有这么一点点的收集癖 ~ 发现好的图片,发现好的书籍,发现各种能存放在电脑上的东西,都喜欢把它批量的爬取下来. 然后放着 ...

  5. python多线程爬取斗图啦数据

    python多线程爬取斗图啦网的表情数据 使用到的技术点 requests请求库 re 正则表达式 pyquery解析库,python实现的jquery threading 线程 queue 队列 ' ...

  6. 使用selenium 多线程爬取爱奇艺电影信息

    使用selenium 多线程爬取爱奇艺电影信息 转载请注明出处. 爬取目标:每个电影的评分.名称.时长.主演.和类型 爬取思路: 源文件:(有注释) from selenium import webd ...

  7. Python爬虫入门教程 13-100 斗图啦表情包多线程爬取

    斗图啦表情包多线程爬取-写在前面 今天在CSDN博客,发现好多人写爬虫都在爬取一个叫做斗图啦的网站,里面很多表情包,然后瞅了瞅,各种实现方式都有,今天我给你实现一个多线程版本的.关键技术点 aioht ...

  8. Python爬虫入门教程 11-100 行行网电子书多线程爬取

    行行网电子书多线程爬取-写在前面 最近想找几本电子书看看,就翻啊翻,然后呢,找到了一个 叫做 周读的网站 ,网站特别好,简单清爽,书籍很多,而且打开都是百度网盘可以直接下载,更新速度也还可以,于是乎, ...

  9. ubuntu中eclipse 不支持gbk编码问题解决办法

    今天在ubuntu 下, 把Windows下工程导入Linux下Eclipse中,由于工程代码,是GBK编码,而Ubuntu默认不支持GBK编码,所以,要让Ubuntu支持GBK. 方法如下: 1.修 ...

随机推荐

  1. 燕十八MySQL优化学习笔记

    观察 show status; 里面的这三个参数;Queries Threads_connected Threads_running判断周期性变化 -------------------------- ...

  2. Linux中执行shell脚本的4种方法总结

    bash shell 脚本的方法有多种,现在作个小结.假设我们编写好的shell脚本的文件名为hello.sh,文件位置在/data/shell目录中并已有执行权限. 方法一:切换到shell脚本所在 ...

  3. [ActionScript 3.0] 获取随机颜色

    import flash.display.MovieClip; import flash.geom.ColorTransform; //方法一 var red:Number = Math.random ...

  4. [AIR] 获取U盘,打开U盘

    示例: 获取存储卷的方法: package com.controls { import flash.events.StorageVolumeChangeEvent; import flash.file ...

  5. javascript type操作

    var a; type a;//结果为undefined type b;//结果也是undefinedalert(a);//弹出undefinedalert(b);//错误代码  上述代码,type ...

  6. SpringMVC 自动封装枚举类的方法

    springmvc默认无法自动封装枚举类,解决方法如下: 1.枚举类 public enum GoodsPromoteEnum { /** * 0 精品 */ fine("精品", ...

  7. 【VB6】使用VB6创建和访问Dom树【爬虫基础知识 】

    使用VB6创建和访问Dom树 关键字:VB,DOM,HTML,爬虫,IHTMLDocument 我们知道,在VB中一般大家会用WebBrowser来获取和操作dom对象. 但是,有这样一种情形,却让我 ...

  8. XML Schema命名空间解析

    URI Web中汇集了各种资源.资源可以是具有标识的任何事物, 如文档. 文件. 菜单项. 计算机. 服务等, 甚至可以包括人. 组织和概念[BernersLee 1998].在Web体系结构中, ...

  9. 2015.05.14:codesmith

    安装: 安装好codesmith会有两个软件:一个编译器(CodeSmith Generator Explorer),一个生成工具(CodeSmith Generator Studio) 破解: 一般 ...

  10. JAVA 常用框架和工具

    集成开发工具(IDE):Eclipse.MyEclipse.Spring Tool Suite(STS).Intellij IDEA.NetBeans.JBuilder.JCreator JAVA服务 ...