# -*- coding: utf-8 -*-
"""
Created on Sat Oct 22 09:27:18 2016
"""
Created on Sat Oct 22 09:27:18 2016
@author: hhxsym
"""
"""
import os
import urllib
import urllib2
import re
import urllib2
import re
inpath="C:\\Users\\hhxsym\\Desktop\\课程群Python爬虫"
inpath = unicode(inpath , "utf8")
os.chdir(inpath) #不做编码转换后,中文路径无法打开,更改
inpath = unicode(inpath , "utf8")
os.chdir(inpath) #不做编码转换后,中文路径无法打开,更改
def loaddown_jpg(url):
request = urllib2.Request(url) #请求
response = urllib2.urlopen(request) #打开请求对象
print response.getcode() #打开状态码,如果是200,请求成功
html = response.read().decode("UTF-8") # .decode解码, 网页有多种编码方式,要根据网页的编码方式进行解码,否则,容易出现乱码
string = r'src="(http://imgsrc\.baidu\.com.+?\.jpg)" pic_ext="jpeg"' #建议加r,r 表示原始字符串,()分组,提取需要的内容
urls = re.findall(string, html) #获取所有的图片地址 re.findall(正则表达式,查询的内容)
#print urls
x=0
for url in urls:
urllib.urlretrieve(url,"%s.jps" % x ) #遍历下载所有的图片,urllib.urlretrieve(要下载的地址, 文件名字)
x += 1
print url
request = urllib2.Request(url) #请求
response = urllib2.urlopen(request) #打开请求对象
print response.getcode() #打开状态码,如果是200,请求成功
html = response.read().decode("UTF-8") # .decode解码, 网页有多种编码方式,要根据网页的编码方式进行解码,否则,容易出现乱码
string = r'src="(http://imgsrc\.baidu\.com.+?\.jpg)" pic_ext="jpeg"' #建议加r,r 表示原始字符串,()分组,提取需要的内容
urls = re.findall(string, html) #获取所有的图片地址 re.findall(正则表达式,查询的内容)
#print urls
x=0
for url in urls:
urllib.urlretrieve(url,"%s.jps" % x ) #遍历下载所有的图片,urllib.urlretrieve(要下载的地址, 文件名字)
x += 1
print url
if __name__=='__main__': #用来调试代码模块,2开头是请求成功
loaddown_jpg("http://tieba.baidu.com/p/3797994694")
loaddown_jpg("http://tieba.baidu.com/p/3797994694")