最近开始学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
转载于:https://blog.51cto.com/statby/1569640