没得事就爬一下我喜欢的海贼王上的图片

需要在d盘下建立一个imgcache文件夹

# -*- coding: utf-8 -*-

import urllib

import urllib2

import json

from bs4 import BeautifulSoup

import threadpool

import thread

class htmlpaser:

def __init__(self):

self.url='http://1.hzfans.sinaapp.com/process.php'

#POST数据到接口

def Post(self,postdata):

# headers = {

# 'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'

# }

# data = urllib.urlencode(postdata)

# req = urllib2.Request(self.url,data,headers)

# resp = urllib2.urlopen(req,None,20)

# html = resp.read()

# return html

data = urllib.urlencode(postdata)

req = urllib2.Request(url, data)

html= urllib2.urlopen(req).read()

print html

#获取html内容

def GetHtml(self,url):

headers = {

'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'

}

req = urllib2.Request(url,None,headers)

resp = urllib2.urlopen(req,None,5)

html = resp.read()

#return html.decode('utf8')

return html

def GetHtml2(self,url):

page = urllib.urlopen(url)

html = page.read()

page.close()

return html

def GetHtml3(self,url):

req_header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',

'Accept':'text/html;q=0.9,*/*;q=0.8',

'Accept-Charset':'ISO-8859-1,utf-8;q=0.7,*;q=0.3',

'Accept-Encoding':'gzip',

'Connection':'close',

'Referer':None #注意如果依然不能抓取的话,这里可以设置抓取网站的host

}

req_timeout = 5

req = urllib2.Request(url,None,req_header)

resp = urllib2.urlopen(req,None,req_timeout)

html = resp.read()

return html

def GetList(self,html):

soup = BeautifulSoup(''.join(html))

baseitem=soup.find('ul',{'class':'list'})

slist=baseitem.select('li a')

return slist

def DownImg(self,imgurl):

path= r"d:/imgcache/"+self.gGetFileName(imgurl)

data = urllib.urlretrieve(imgurl,path)

return data

def gGetFileName(self,url):

if url==None: return None

if url=="" : return ""

arr=url.split("/")

return arr[len(arr)-1]

def mkdir(path):

import os

path=path.strip()

path=path.rstrip("\\")

# 判断路径是否存在

# 存在 True

# 不存在 False

isExists=os.path.exists(path)

# 判断结果

if not isExists:

# 如果不存在则创建目录

# 创建目录操作函数

os.makedirs(path)

return True

else:

# 如果目录存在则不创建,并提示目录已存在

return False

#返回两个值

def ParseContent(self,html):

soup = BeautifulSoup(''.join(html))

baseitem=soup.find('div',{'class':'showbox'})

title=soup.find('div',{'class':'msg'}).find('div',{'class':'m_left'}).get_text()

imglist=baseitem.find_all('img')

for img in imglist:

imgurl=img.get('src')

self.DownImg(imgurl)

content=baseitem.get_text().encode('utf8')

position=content.find('热点推荐')

return title,content[0:position]

def ParseItem(self,item):

url=item.get('href')

if url==None:

return

#print url+'\n'

html=obj.GetHtml2(url)

title,content=obj.ParseContent(html)

#print title+'\n'

return title

def print_result(request, result):

print str(request.requestID)+":"+result

obj=htmlpaser()

pool = threadpool.ThreadPool(10)

for i in range(1,40):

url="http://op.52pk.com/shtml/op_wz/list_2594_%d.shtml"%(i)

html=obj.GetHtml2(url)

items=obj.GetList(html)

print 'add job %d\r' % (i)

requests = threadpool.makeRequests(obj.ParseItem, items, print_result)

[pool.putRequest(req) for req in requests]

pool.wait()

python海贼王logo_Python 实现的下载op海贼王网的图片(网络爬虫)相关推荐

  1. python海贼王logo_Python实现的下载op海贼王网的图片

    没得事就爬一下我喜欢的海贼王上的图片 需要在d盘下建立一个imgcache文件夹 # -*- coding: utf-8 -*- import urllib import urllib2 import ...

  2. Python实训day05pm【JS-DOM-获取元素节点对象、网络爬虫】

    Python实训-15天-博客汇总表 目录 1.CSS选择器 2.网络爬虫 2.1.练习1 2.2.练习2 1.CSS选择器 .rank-body .book-mid-info .author a:n ...

  3. Python 实现的下载op海贼王网的图片(网络爬虫)

    没得事就爬一下我喜欢的海贼王上的图片 须要在d盘下建立一个imgcache目录 # -*- coding: utf-8 -*-import urllib import urllib2import js ...

  4. python serial库文件下载_Pyserial python 串口驱动库pyserial - 下载 - 搜珍网

    Pyserial/ Pyserial/pyserial-2.7.win32.exe Pyserial/pyserial-2.7.win32_py3k.exe Pyserial/pyserial-2.7 ...

  5. python中data.find_all爬取网站为空列表_Python网络爬虫之Scrapy 框架-分布式【第二十九节】...

    1. 介绍scrapy-redis框架 scrapy-redis 一个三方的基于redis的分布式爬虫框架,配合scrapy使用,让爬虫具有了分布式爬取的功能. github地址: https://g ...

  6. python从零基础到项目实战 当当_Python 3.x网络爬虫从零基础到项目实战

    ● 案例完整 本书中的所有案例都是通过 理论讲解 环境搭建 完整代码及分析 运行结果 这种完善的结构进行讲解的.此外,复杂的案例配有项目结构图,有难度的案例还分析了底层源码,并且对于所有案例的讲解,都 ...

  7. Python实训day04pm【网络爬虫(文本、图片)】

    Python实训-15天-博客汇总表 目录 1.网络爬虫 1.1.爬取文本 1.2.爬取图片 2.其他知识点 上午题目讲解 昨天的基础题目讲解 爬取非文本(图片) 1.网络爬虫 #bili 视频,爬下 ...

  8. python爬虫抓取文本_Python实现可获取网易页面所有文本信息的网易网络爬虫功能示例...

    本文实例讲述了Python实现可获取网易页面所有文本信息的网易网络爬虫功能.分享给大家供大家参考,具体如下: #coding=utf-8 #------------------------------ ...

  9. Python 爬虫---(7) Python3网络爬虫快速入门实战解析

    转载请注明作者和出处: http://blog.csdn.net/c406495762  Github代码获取:https://github.com/Jack-Cherish/python-spide ...

最新文章

  1. android aspectj 编译,Android AOP方案(一)——AspectJ
  2. 网速、宽带速率运营商说法与用户说法转换
  3. 正则表达式收藏(四)之MySQL的正则
  4. Destroying Array(并查集)
  5. SpringBoot 使用教程
  6. 微型计算机原理上机实验改错,北京理工大学微机原理汇编语言上机实验题
  7. UkrGuru.SqlJson非常适合您的ASP.NET Core Web应用程序!
  8. java hashmap
  9. 首届 “女生科技体验节” 大爆料!
  10. 阶段5 3.微服务项目【学成在线】_day02 CMS前端开发_19-CMS前端页面查询开发-页面原型-Table组件测试...
  11. 能打开pdf格式的软件
  12. 智商情商哪个重要_情商与智商哪个更重要?三个方面让你彻底明白
  13. Rust 调用标准C接口的自定义c/c++库,FFI详解
  14. 数据源EPMSSqlDataSource的使用
  15. 《管理信息系统》考研复试整理
  16. 牛顿(Newton)方法
  17. 讲讲多拨的额外骚操作(多拨附加教程)
  18. LSwarm:复杂城市场景下覆盖受限的大集群高效避碰(Swarm-2019)
  19. リヴァイア / 鱼妹
  20. mysql数据库历史语句提取_如何从MySQL数据库的不同表中提取创建语句?

热门文章

  1. 飞鸽传书 再次报道,回到90年代中期的业界
  2. 用VC写Assembly代码(7)--在Visual C++中使用内联汇编
  3. 水滴石穿C语言之extern声明辨析
  4. Google 推出免费利器,让编程小白也能开发游戏
  5. 微软 Edge 浏览器将推出 iOS 和 Android 版
  6. 性能优化(6):为什么一定要将css置顶?
  7. python redis事务_python实现redis三种cas事务操作
  8. OpenCV中霍夫圆检测
  9. mysql 无法启动 查看原因_Mysql服务无法启动问题
  10. 从Scratch到C++ 从Scratch到python书籍