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

须要在d盘下建立一个imgcache目录

# -*- coding: utf-8 -*-import urllib
import urllib2import 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 htmldata = 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 htmldef GetHtml2(self,url): page = urllib.urlopen(url) html = page.read() page.close() return htmldef 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 = 5req = urllib2.Request(url,None,req_header)resp = urllib2.urlopen(req,None,req_timeout)html = resp.read()return htmldef GetList(self,html):soup = BeautifulSoup(''.join(html))baseitem=soup.find('ul',{'class':'list'})slist=baseitem.select('li a')return slistdef DownImg(self,imgurl):path= r"d:/imgcache/"+self.gGetFileName(imgurl)data = urllib.urlretrieve(imgurl,path)return datadef gGetFileName(self,url):if url==None: return Noneif url=="" : return ""arr=url.split("/")return arr[len(arr)-1]def mkdir(path):import ospath=path.strip()path=path.rstrip("\\")# 推断路径是否存在# 存在     True# 不存在   FalseisExists=os.path.exists(path)# 推断结果if not isExists:# 假设不存在则创建文件夹# 创建文件夹操作函数os.makedirs(path)return Trueelse:# 假设文件夹存在则不创建,并提示文件夹已存在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)+":"+resultobj=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()

转载于:https://www.cnblogs.com/bhlsheji/p/5152931.html

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

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

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

  2. python海贼王logo_Python 实现的下载op海贼王网的图片(网络爬虫)

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

  3. python小练——下载指定url中的图片

    python小练--下载指定url中的图片 #coding=gbk #download pictures of the url #useage: python downpicture.py www.b ...

  4. Python可以这样学(第十季:网络爬虫实战)-董付国-专题视频课程

    Python可以这样学(第十季:网络爬虫实战)-83人已学习 课程介绍         陆续介绍和分享一些网络爬虫方面的案例,Python基础知识可以参考前面的"Python可以这样学&qu ...

  5. python就业前景不好_Python就业前景好不好?学Python好找工作吗?【附资料全家桶—网络爬虫入门到实践】...

    Python是目前市场上大家听说最多的编程语言吧,0基础小白学什么?当然Python,转行学什么?也是Python--那么Python的发展前景真的好吗?学完好找工作吗带领大家了解一下吧. 1.就业岗 ...

  6. PYTHON 实现 NBA 赛程查询工具(二)—— 网络爬虫

    前言: 第一篇博客,记录一下最近的一点点小成果. 一切的学习都从兴趣开始.最近忽然想学习一下 pyqt 和 python 的网络爬虫知识,于是就自己找了一个课题做了起来.因为我正好是个 NBA 球迷, ...

  7. 用Python代码来下载任意指定网易云歌曲

    前两天教了大家如何在控制台上找到真实的mp3播放地址,但是不可以下载付费的,因为只能下载可播放的歌曲.至于怎样下载付费网易云音乐,还是开个会员吧,要知道免费是最贵的的这个道理. 有粉丝看了前两天文章想 ...

  8. 用Python代码来下载任意指定网易云歌曲(超详细版)

    前两天教了大家如何在控制台上找到真实的mp3播放地址,但是不可以下载付费的,因为只能下载可播放的歌曲.至于怎样下载付费网易云音乐,还是开个会员吧,要知道免费是最贵的的这个道理. 有粉丝看了前两天文章想 ...

  9. Python Pyside2 Pinger新手QQ音乐网易云酷狗爬虫下载

    此文记录了Pinger优化2.0版本,仅此作为学习Python的动力: Pinger 1.0请看前一篇的博客! Pyside2新手1400行代码QQ音乐网易云酷狗轻松下载 优化一: 更改Pinger的 ...

最新文章

  1. 《OpenCV3编程入门》学习笔记8 图像轮廓与图像分割修复(四)图像的矩
  2. Fiddler抓取https的设置
  3. Internet Explorer 已停止工作 解决办法
  4. nfs服务器安装与客户端安装
  5. delphi指针简单入门
  6. C# 9.0 新功能一览!
  7. 印花固浆在水性印花中的作用
  8. 修改帝国cms验证码 每个都不一样
  9. 无法使用SQL login去登陆SQL Server - 'Password did not match'
  10. 大学物理公式和名词整理
  11. Python学习,55道django面试题,来试试吧
  12. Cartographer 源码解析1.1 —— 算法整体结构
  13. SLAM学习 | 使用小觅相机MYNTEYE-S1030收集数据集
  14. vue 图片画框获取坐标
  15. 最小采样频率计算公式_音频文件大小计算公式-好文转载
  16. “我一年赚100万,这个项目告诉你也无妨!”
  17. 电子设计PCB-Altium笔记
  18. UE4人物——导入多个动作流程笔记(从Mixamo)
  19. 教学管理系统-实验四 顺序图
  20. hihoCoder1290. Demo Day

热门文章

  1. Linux 安装 Pycharm
  2. 第二次作业——小学生四则运算
  3. ASP.NET Web开发框架之七 开发流程与模式
  4. ASP页面中访问基于.net的COM组件[转]
  5. python api调用百度ai平台_Python 百度AI接口调用
  6. (01)System Verilog 程序数据采集
  7. (35)System Verilog类静态变量详解
  8. c语言词典课程设计报告,C语言课程设计英语词典排版系统
  9. python3默认的字符编码和文件编码_Python的字符编码之三个问题
  10. 2021-03-10 模板扩展类调用模板基类成员函数