python爬取妹子图片1

'''
`# -*- coding:utf-8 -*-
import urllib
import urllib2
from bs4 import BeautifulSoup
url="http://www.mzitu.com/all"
request=urllib2.Request(url)
response=urllib2.urlopen(request)
start_html=response.read()
soup=BeautifulSoup(start_html,'lxml')
all_a=soup.find('div',class_='all').find_all('a')
for a in all_a:
    href=a['href']
    #print href
    html=urllib.urlopen(href)
    html_soup=BeautifulSoup(html,'lxml')
    max_span=html_soup.find_all('span')[10].string
    for page in range(1,int(max_span)+1):
        page_url=href+'/'+str(page)
        img_html=urllib.urlopen(page_url)
        img_soup=BeautifulSoup(img_html,'lxml')
        #find('div',claa_="main_img"):是在所有的div中查找class属性为main-img的div,下面的意思在已经找到的那个div中再查找img标签
        img_url=img_soup.find('div',class_='main-image').find('img')['src']    #find是只查找一个结果之后就找了,因此不是一个列表的类型,可以直接索引
        print img_url
        x=img_url[-6:-4]
        urllib.urlretrieve(img_url,"g:\\img\\%s.jpg"%x)
[python官方文档](http://python.usyiyi.cn/translate/python_278/library/index.html)
'''
`









你可能感兴趣的:(爬虫,python,python爬虫)