使用python抓取美空模特图片

作者:cnchanghai , 分类:编程学习 , 浏览:480 , 评论:

代码原理:

从http://www.moko.cc/mtb.html页面上抓取到每一个 属性类为sub_show 的div,并遍历抓到的div段 找出其中待“html”字段的超链接。在这里我把第一个和最后一个div段跳过了,因为第一个是外国模特(包含男模特),最后一个是男模特。从抓取到的链接里面,我只挑选了第一个链接,没有做遍历。

根据刚才挑选的连接抓取其中的div class=thumbnail_box 中的所有dd中img的src属性;并把src中?号与之后的部分删除,并输出。  后续可能会将这些图片地址保存到数据库中或者将图片下载下来


代码段:

#!/usr/bin/python
#coding:utf-8
from bs4 import BeautifulSoup
import requests
url='http://www.moko.cc/mtb.html'
r=requests.get(url, verify=False)
content=r.content
soup=BeautifulSoup(content,'lxml')
modlist=soup.find_all('div','sub_show')
link=[]
for i in modlist:
    if i==modlist[-1] or i==modlist[0]:
        continue
    tmp=i.find_all('a')
    for j in tmp:
        if 'html' in j['href']:
            link.append(j['href'])
            print j['href']
print 'http://www.moko.cc'+link[0]
url2='http://www.moko.cc'+link[0]
r=requests.get(url2, verify=False)
content=r.content
soup=BeautifulSoup(content,'lxml')
alist=soup.find_all('div','thumbnail_box')
soup2=BeautifulSoup(str(alist),'lxml')
blist=soup2.find_all('dd')
for item in blist:
    link=item.find('img')
    try:
         if '.png' in link['src']:
              pass   
         else:
              print link['src'].split('?')[0]
    except:
        continue

执行结果:

http://www.moko.cc/mtb/model/20175/space.html
http://img1qn.moko.cc/2016-11-03/af4e6781-73d6-4fd9-bc22-48b92a111da1.jpg
http://img1qn.moko.cc/2016-07-15/5fceeeaa-e155-406e-8711-4c55f346f0d0.jpg
http://img1qn.moko.cc/2016-07-15/9d899af8-8d17-4fc3-b99b-0cf8334b3ead.jpg
http://img1qn.moko.cc/2016-07-15/777ea52b-787f-42fc-ba16-5d0e7f4feb7f.jpg
http://img1qn.moko.cc/2016-07-15/ae5ec646-09fd-4ef9-baf2-027ba2a04fda.jpg
http://img1qn.moko.cc/2016-07-15/3fac2872-48dd-496d-97e8-74e77dce782d.jpg
http://img1qn.moko.cc/2016-06-01/21d36ae4-5386-456a-afea-746b19a90a4d.jpg
http://img1qn.moko.cc/2016-06-01/c858ab87-92fd-41e4-b868-2a133cb8c2ef.jpg
http://img1qn.moko.cc/2016-06-01/03d61fa7-e88a-48be-a500-79308ba1066c.jpg
http://img1qn.moko.cc/2016-06-01/d9bdf409-83ed-4701-a1e9-d78c5e63a7fe.jpg
http://img1qn.moko.cc/2016-06-01/a3db905e-e98d-42f3-92e6-454be8f1982e.jpg
http://img1qn.moko.cc/2016-06-01/9142e838-ea30-45a7-b30b-60597a6768af.jpg
http://img1qn.moko.cc/2016-06-01/aa6a3d0b-63bf-4a89-bea8-4f1ff6f4e932.jpg
http://img1qn.moko.cc/2016-04-25/553ef651-ea55-4fc0-b18f-9a2a73ebb5a5.jpg
http://img1qn.moko.cc/2016-04-25/7dc7e715-02bf-498c-8e9b-e261b752b374.jpg
http://img1qn.moko.cc/2016-04-25/e6072efd-a8a0-4f37-af25-bafa8b337091.jpg
http://img1qn.moko.cc/2016-04-24/5a70ce54-2757-444f-89ef-4c2724882d70.jpg
http://img1qn.moko.cc/2016-04-23/a740edd5-daae-4bf5-be24-e2b5140a5613.jpg
http://img1qn.moko.cc/2016-04-23/2e1bfe2f-3916-45a1-a03e-925f5eb80354.jpg
http://img1qn.moko.cc/2016-04-23/944222a1-b937-4752-80ce-c0a6069e029c.jpg
http://img1qn.moko.cc/2016-03-14/6c77696a-776a-455a-ae3f-4e3e5eb851af.jpg
http://img1qn.moko.cc/2016-03-14/f82f4ac7-9d13-451f-8213-18a82ce021b4.jpg
http://img1qn.moko.cc/2016-03-14/ea20ec7b-055e-4a3c-8b7b-30b0222f6ff1.jpg
http://img1qn.moko.cc/2016-03-14/5a108766-ad3a-4783-b231-ecf669b6fc1f.jpg
http://img1qn.moko.cc/2016-03-14/0ee56481-e14c-4bc6-8a82-e3877e4c8388.jpg
http://img1qn.moko.cc/2016-03-14/4e1544e2-6d0c-4015-adce-8b31dbb81736.jpg
http://img1qn.moko.cc/2016-03-14/37a201dd-4512-4ad9-960e-d3ca17c7aea2.jpg
http://img1qn.moko.cc/2016-03-14/1c602c4c-0dbb-429b-a13b-d279a3acad48.jpg
http://img1qn.moko.cc/2016-03-14/5c641287-0a85-4fe5-bf50-52caef8264b0.jpg
http://img1qn.moko.cc/2016-03-14/432bd3f6-4105-4a71-ad25-0304f3e620be.jpg
http://img1qn.moko.cc/2016-03-14/84f2ad03-3381-4334-872e-14d6090374b1.jpg
http://img1qn.moko.cc/2016-03-14/95b4a2e6-c33a-4467-b9ac-c719c25b1d7f.jpg
http://img1qn.moko.cc/2016-03-14/c148ad86-b17c-4cec-9201-6bcc2b7f59b1.jpg
http://img1qn.moko.cc/2016-03-14/8b210859-2f95-4bfb-99eb-ab053182b0ea.jpg
http://img1qn.moko.cc/2016-03-14/790e274e-916d-47f6-9705-91e8fe9350be.jpg
http://img1qn.moko.cc/2016-03-10/23f08d71-d9cf-443a-8546-fe02a81d20d7.jpg
http://img1qn.moko.cc/2016-03-07/0abefe37-b3e9-40bb-a236-e1809273abf0.jpg
http://img1qn.moko.cc/2016-03-07/96497abc-2a51-4540-bd56-d01879e1b904.jpg
http://img1qn.moko.cc/2016-03-07/c6afff9b-f1a9-4fde-a227-a383291895c3.jpg
http://img1qn.moko.cc/2016-03-07/f788ba0f-448d-4610-8757-1573053b180a.jpg
http://img1qn.moko.cc/2016-03-07/88f981b3-b3c8-42fd-b386-1930c02da348.jpg
http://img1qn.moko.cc/2016-03-07/befd729c-b5ff-459b-b796-20c8824c9f11.jpg
http://img1qn.moko.cc/2016-03-07/f0464bef-dd30-4384-8868-0cca6b23a146.jpg
http://img1qn.moko.cc/2016-03-07/89a18629-d677-4b40-b3b8-ab9f521e6b2e.jpg
http://img1qn.moko.cc/2016-03-07/ed9aa5ba-a988-440f-b580-bef782031562.jpg
http://img1qn.moko.cc/2016-03-07/1a3545c1-ba0b-4d11-9eba-e26c439c024e.jpg
http://img1qn.moko.cc/2016-03-07/7a2d4687-f6eb-4529-9b8f-db3a68c8a3a3.jpg
http://img1qn.moko.cc/2016-03-07/b819ebb3-820c-447f-a084-2454b997fa29.jpg
http://img1qn.moko.cc/2016-03-07/0d090371-bbc9-4c39-b130-d4c6a6d6835f.jpg
http://img1qn.moko.cc/2016-03-07/a4a3411f-2ddf-43c4-a0e3-6a833f9a45ca.jpg
http://img1qn.moko.cc/2016-03-07/dd3a5f83-1bd0-4de0-abe9-283027698bd5.jpg
http://img1qn.moko.cc/2016-03-07/335fc51e-4e19-449b-ba91-bf2f46ef2bcd.jpg
http://img1qn.moko.cc/2016-03-07/81f964c1-da2c-4efe-8542-17abe4dac4b3.jpg
http://img1qn.moko.cc/2016-03-07/b3af0f24-655d-4900-8f4a-08e888acdd26.jpg
http://img1qn.moko.cc/2016-03-07/fac487c3-19b2-429f-857d-383449e91b89.jpg
http://img1qn.moko.cc/2016-03-07/5314cac2-29c6-4e41-93d5-c61c10f6d9de.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_9115291.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_10657051.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_10657039.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_10657038.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_10657022.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_10657007.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_10657006.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_10657005.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_10657004.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_10657002.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_10656945.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_10656942.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_10425714.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_9629736.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_9629693.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_10425699.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_9629721.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_10425703.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_10425700.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_10425698.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_10425696.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_10425695.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_10425594.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_10425548.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_10425528.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_9629688.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_10425503.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_10425497.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_9629712.jpg
http://img2.moko.cc/users/0/67/20175/mtb/img2_src_10657008.jpg

顺便插入一张图片http://img2.moko.cc/users/0/67/20175/mtb/img2_src_10657008.jpg


李长海的博客