File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11# -*- coding:utf-8 -*-
22from lxml import etree
33import requests
4+ import urllib
45
56__author__ = 'monkey'
67
78# 题目要求:
89# 用Pyhton写一个爬图片的程序,爬这个链接里的日本妹子图片
910# 地址:http://tieba.baidu.com/p/2166231880
1011
12+ # 获取url地址,对页面进行爬去
1113def spider (url ):
1214 html = requests .get (url )
1315 selector = etree .HTML (html .text )
1416 picitems = []
15- picitems = selector .xpath ('//div[@id="post_content_29397251028"]' )
17+ picitems = selector .xpath ('//div[@id="post_content_29397251028"]/img[@class="BDE_Image"]' )
18+ print (len (picitems ));
19+
20+ i = 0
21+ for pic in picitems :
22+ url = pic .xpath ('@src' )[0 ]
23+ print (url )
24+ dir = './%d.jpg' % i
25+ download_Image (url , dir )
26+ i += 1
27+
28+
29+
30+ def download_Image (url , save_path ):
31+ urllib .request .urlretrieve (url , save_path )
1632
17- print (len (picitems ))
1833
1934if __name__ == '__main__' :
2035 url = "http://tieba.baidu.com/p/2166231880"
You can’t perform that action at this time.
0 commit comments