当前位置: 移动技术网 > IT编程>脚本编程>Python > python爬取妹子图片

python爬取妹子图片

2018年04月21日  | 移动技术网IT编程  | 我要评论

天津南开中学官网,星光大道肖蕾,凌源吧迟丽丽

`# -*- coding:utf-8 -*-
import urllib
import urllib2
from bs4 import BeautifulSoup
url="https://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('p',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('p',claa_="main_img"):是在所有的p中查找class属性为main-img的p,下面的意思在已经找到的那个p中再查找img标签
        img_url=img_soup.find('p',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官方文档](https://python.usyiyi.cn/translate/python_278/library/)

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网