python3.5.2爬虫

Posted 0x0c,0x0d

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python3.5.2爬虫相关的知识,希望对你有一定的参考价值。

话不多说,都在代码里

#下载斗鱼颜值栏目主播照片

#author:ives

#date:2016-8-28 21:58

#e-mail:[email protected]

 

import urllib.request

import string

import re

import json

import sys,os

 

url="http://capi.douyucdn.cn/api/v1/getColumnRoom/8?offset="

urlAfter="&limit=30&client_sys=android"

offset=0

#下载的张数

count=1

 

#获取当前脚本路径

def cur_file_dir():

path=sys.path[0]

if os.path.isdir(path):

return path

elif os.path.isfile(path):

return os.path.dirname(path)

 

#获取网页json

def gethtml(url):

page=urllib.request.urlopen(url)

html=page.read()

#解决编码问题

return html.decode("UTF-8")

 

#下载图片

def downLoadImg(url):

#截取文件名

file=open(downLoadUrl+url.split("/")[-1],‘wb‘)

img=urllib.request.urlopen(url)

buf=img.read()

file.write(buf)

return

 

#动态生成路径

def getLink(url):

response=getHtml(url)

#处理中文编码问题

response = response.encode(‘latin-1‘).decode(‘unicode_escape‘)

jsonText=json.loads(response)

#获取所有的数据

test=jsonText["data"]

for i in test:

#获取图片路径

src=i["vertical_src"]

downLoadImg(src)

global count

print("已下载"+str(count)+"张"+src+"-*-"+str(offset))

count=count+1

return

######################################################

 

#获取下载目录

downLoadUrl=cur_file_dir()+"/"

 

#动态修改offset获取更多图片

while(True):

finalUrl=url+str(offset)+urlAfter

getLink(url)

offset=offset+20

 

 

 

 

联系我:[email protected]

2016-8-30

21:49

以上是关于python3.5.2爬虫的主要内容,如果未能解决你的问题,请参考以下文章

centos6.8安装python3.5.2

通过配置环境变量,直接运行python3.5.2

python3.5.2库getpass

如何在 CentOS 上将 Python3.5.2 设置为默认 Python 版本?

Linux下安装Python3.5.2Djangoparamiko

linux(CentOS)下升级python3.5.2