Skip to content

Commit bddb61a

Browse files
committed
第0013题
1 parent 63a5d92 commit bddb61a

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

monkey/0013/main.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
11
# -*- coding:utf-8 -*-
22
from lxml import etree
33
import requests
4+
import urllib
45

56
__author__ = 'monkey'
67

78
# 题目要求:
89
# 用Pyhton写一个爬图片的程序,爬这个链接里的日本妹子图片
910
# 地址:http://tieba.baidu.com/p/2166231880
1011

12+
# 获取url地址,对页面进行爬去
1113
def 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

1934
if __name__ == '__main__':
2035
url = "http://tieba.baidu.com/p/2166231880"

0 commit comments

Comments
 (0)