python shell 爬虫 妹子图片
最近开始学Python,人们学一门语言潜移默化的第一个命令往往那个就是 hello world!
这里也差不多,用python写了个小爬虫,爬妹子图的"照片".感觉效率好慢,应该是代码的问题,
在此献丑了!!!
需要安装两个好用的库,分别是 requests 和 Beautifulsoup,安装十分简单回头补上!
嗯~就这样
代码在 Python 2.7.8 以及 Python 3.4.1 下都可以测试通过!
#coding:utf-8 import requests from bs4 import BeautifulSoup import re DownPath = "/jiaoben/python/meizitu/pic/" import urllib head = {‘User-Agent‘:‘Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6‘} TimeOut = 5 PhotoName = 0 c = ‘.jpeg‘ PWD="/jiaoben/python/meizitu/pic/" for x in range(1,4): site = "http://www.meizitu.com/a/qingchun_3_%d.html" %x Page = requests.session().get(site,headers=head,timeout=TimeOut) Coding = (Page.encoding) Content = Page.content#.decode(Coding).encode(‘utf-8‘) ContentSoup = BeautifulSoup(Content) jpg = ContentSoup.find_all(‘img‘,{‘class‘:‘scrollLoading‘}) for photo in jpg: PhotoAdd = photo.get(‘data-original‘) PhotoName +=1 Name = (str(PhotoName)+c) r = requests.get(PhotoAdd,stream=True) with open(PWD+Name, ‘wb‘) as fd: for chunk in r.iter_content(): fd.write(chunk) print ("You have download %d photos" %PhotoName)
#在放一个以前用shell写的下载煎蛋妹子图片的代码吧!
页码自己嵌套一个for 就可以下载多点妹子图了~懒得弄了!
#!/bin/bash DATE=`date +%y%m%d-%H:%M` PITCURE_ADDRESS="/jiaoben/python/meizitu/pic" BROWSER="Mozil5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0" read -p "down number: " page WEBSITE="http://jandan.net/ooxx/page-"${page}"" SOURCE_WEBSITE="http://jandan.net/ooxx" #read -p "down number: " page #photo=`curl -A $"BROWSER" -m 10 -e $SOURCE_WEBSITE $WEBSITE |awk -F"://" ‘/.jpg/ {print $3}‘ |awk ‘{FS="\""}{print $1}‘ ` #curl -A "Mozil5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0" -m 10 -e "http://jandan.net/ooxx" "http://jandan.net/ooxx/page-1210" |awk -F"://" ‘/.jpg/ {print $3}‘|awk -F"\"" ‘{print $1}‘ photo=`curl -A $"BROWSER" -m 10 -e $SOURCE_WEBSITE $WEBSITE |awk -F"://" ‘/.jpg/ {print $3}‘|awk -F"\"" ‘{print $1}‘` number=`$photo|wc -l` for i in $photo do wget -q -T 10 -P $PITCURE_ADDRESS $i >/dev/null name=‘echo ${i##*/}‘ number=$((number+1)) /bin/mv ${PITCURE_ADDRESS}${i##*/} ${PITCURE_ADDRESS}$page-$number.jpg done
本文出自 “statby的博客” 博客,请务必保留此出处http://statby.blog.51cto.com/7588140/1569640
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。