python爬虫学习(1)__抓取煎蛋图片
#coding=utf-8
#python_demo 爬取煎蛋妹子图在本地文件夹
import requests
import threading
import time
import os
from bs4 import BeautifulSoup
#伪造头文件
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
'Accept-Encoding': 'gzip',
'Cookie': '1024679722=aada4mZxRMxqvInd7D6PSgq%2FIkpGFeGlZWAH1gqP8Q; __auc=57bffd35154a91de3cd5d3b1ddb; 1024679722=ebeaLZUFikSR1OE6lm5MJYJSV0V1DbcooxQr0CHu; jdna=596e6fb28c1bb47f949e65e1ae03f7f5#1467948344088; Hm_lvt_fd93b7fb546adcfbcf80c4fc2b54da2c=1467001661,1467189261,1467685014,1467857178; Hm_lpvt_fd93b7fb546adcfbcf80c4fc2b54da2c=1467948345; _ga=GA1.2.1739476572.1438849462; _gat=1'}
def saveImgs(*allUrl):
if not os.path.exists('/home/zhanyunwu/jiandanpic'):
os.mkdir('/home/zhanyunwu/jiandanpic') #在本地新建文件夹
print allUrl
if len(allUrl)!=0:
print '当前页面有', len(allUrl), '张图片即将下载'
for l in allUrl:
filename='/home/zhanyunwu/jiandanpic/'+parseName(l)
saveImg(l,filename)
time.sleep(1)
else:
print '当前页面无图片下载'
def saveImg(url,filename):
print '当前图片url:',str(url),'当前图片名称',filename
# u=urllib2.urlopen(url)
# data=u.read()
reponse=requests.get(str(url),headers=headers)
image=reponse.content
# f=open(filename,'wb')
with open(filename,'wb') as f:
f.write(image) def parseName(url):
u=str(url).split('.')
filename=str(url)[30:55]+'.'+u[-1]
return filename
#getallImgUrl
def getAllImgUrl(url):
allurl = []
req=requests.get(url,headers=headers)
# print req.status_code
if req.status_code !=200:
return allurl
soup=BeautifulSoup(req.content,"lxml")
links=soup.select('ol.commentlist img')
print links
for l in links:
allurl.append(l.attrs.get('src'))
return allurl
#多线程爬取
def crawler(n,m):
for l in range(n,m):
url = 'http://jandan.net/ooxx/page-' + str(l) + '#comments'
u=getAllImgUrl(url)
saveImgs(*u)
c1=threading.Thread(target=crawler,args=(1850,1900))
c2=threading.Thread(target=crawler,args=(1950,2000))
c3=threading.Thread(target=crawler,args=(2001,2064))
c1.start()
c2.start()
c3.start()
c1.join()
c2.join()
c3.join()
print 'success'

python爬虫学习(1)__抓取煎蛋图片的更多相关文章
- python爬虫学习(2)__抓取糗百段子,与存入mysql数据库
import pymysql import requests from bs4 import BeautifulSoup#pymysql链接数据库 conn=pymysql.connect(host= ...
- python爬虫学习:分布式抓取
前面的文章都是基于在单机操作,正常情况下,一台机器无论配置多么高,线程开得再多,也总会有一个上限,或者说成本过于巨大.因此,本文将提及分布式的爬虫,让爬虫的效率提高得更快. 构建分布式爬虫首先需要有多 ...
- Python爬虫学习笔记之抓取猫眼的排行榜
代码: import json import requests from requests.exceptions import RequestException import re import ti ...
- Golang分布式爬虫:抓取煎蛋文章|Redis/Mysql|56,961 篇文章
--- layout: post title: "Golang分布式爬虫:抓取煎蛋文章" date: 2017-04-15 author: hunterhug categories ...
- Python爬虫实战四之抓取淘宝MM照片
原文:Python爬虫实战四之抓取淘宝MM照片其实还有好多,大家可以看 Python爬虫学习系列教程 福利啊福利,本次为大家带来的项目是抓取淘宝MM照片并保存起来,大家有没有很激动呢? 本篇目标 1. ...
- Python爬虫实战六之抓取爱问知识人问题并保存至数据库
大家好,本次为大家带来的是抓取爱问知识人的问题并将问题和答案保存到数据库的方法,涉及的内容包括: Urllib的用法及异常处理 Beautiful Soup的简单应用 MySQLdb的基础用法 正则表 ...
- 一次Python爬虫的修改,抓取淘宝MM照片
这篇文章是2016-3-2写的,时隔一年了,淘宝的验证机制也有了改变.代码不一定有效,保留着作为一种代码学习. 崔大哥这有篇>>小白爬虫第一弹之抓取妹子图 不失为学python爬虫的绝佳教 ...
- [Java]使用HttpClient实现一个简单爬虫,抓取煎蛋妹子图
第一篇文章,就从一个简单爬虫开始吧. 这只虫子的功能很简单,抓取到”煎蛋网xxoo”网页(http://jandan.net/ooxx/page-1537),解析出其中的妹子图,保存至本地. 先放结果 ...
- python爬虫学习01--电子书爬取
python爬虫学习01--电子书爬取 1.获取网页信息 import requests #导入requests库 ''' 获取网页信息 ''' if __name__ == '__main__': ...
随机推荐
- 使用SqlBulkCopy批量插入多条数据进入表中
由于工作中项目需求结算一次生成一批相同批次号的数据插入一个表中,然后再通过另一页面展示出来,所以需要用到一次性插入一批数据,所以就采用了SqlBulkCopy插入一批数据 1 public stati ...
- windows平台 culture name 详细列表
点击打开链接http://msdn.microsoft.com/zh-cn/goglobal/bb896001.aspx LCID Culture Identifier Culture Name Lo ...
- css设置文字不换行,超过的部分用“...”代替
设置文字不换行,超过的部分用“...”代替 overflow: hidden; text-overflow: ellipsis; white-space: nowrap; width: /*so ...
- 浏览器中输入URL到返回页面的全过程
第一步,解析域名,找到主机IP (1)浏览器会缓存DNS一段时间,一般2-30分钟不等.如果有缓存,直接返回IP,否则下一步. (2)缓存中无法找到IP,浏览器会进行一个系统调用,查询hosts文件. ...
- linux基础之Shell Script入门介绍
本文介绍下,学习shell script编程的入门知识,通过几个入门实例,带领大家走进shell script的神圣殿堂,呵呵,有需要的朋友参考下. 本文转自:http://www.jbxue.com ...
- Delphi实用小function
Write Log // strLog : the log content need write to log file; // strFile: the compliete file path(in ...
- 简洁代码---python---string
判断字符串是否是回文的,即是否中心对称的: >>>return s[::-1] == s 对称返回True, 否则返回False
- Length of Last Word | Leetcode
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- BZOJ 1754: [Usaco2005 qua]Bull Math
Description Bulls are so much better at math than the cows. They can multiply huge integers together ...
- LibLinear(SVM包)的MATLAB安装
LibLinear(SVM包)的MATLAB安装 1 LIBSVM介绍 LIBSVM是众所周知的支持向量机分类工具包(一些支持向量机(SVM)的开源代码库的链接及其简介),运用方便简单,其中的核函数( ...