首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Python基础代码运用

Python是一种计算机程序设计语言。你可能已经听说过很多种流行的编程语言,比如非常难学的C语言,非常流行的Java语言,适合初学者的Basic语言,适合网页编程的JavaScript语言等等。

那Python是一种什么语言?

首先,我们普及一下编程语言的基础知识。用任何编程语言来开发程序,都是为了让计算机干活,比如下载一个MP3,编写一个文档等等,而计算机干活的CPU只认识机器指令,所以,尽管不同的编程语言差异极大,最后都得“翻译”成CPU可以执行的机器指令。而不同的编程语言,干同一个活,编写的代码量,差距也很大。

Python基础代码:

def download_the_av(url): req = urllib2.Request(url) content = urllib2.urlopen(req).read() while len(content)") title_end=content.find("") title=content[title_begin+7:title_end-14] title=title.replace('/','_') title=filter(lambda x:x in "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ _-",title) quality=['720','480','240'] for i in quality: find_position=content.find("\"quality\":\""+i+"\"") if find_position>0: print "Quality: "+i+"P" break to_find=content[find_position:find_position+4000] pattern=re.compile(r"\"videoUrl\":\"[^\"]*\"") match = pattern.search(to_find) if match: the_url=match.group() the_url=the_url[12:-1]#the real url the_url=the_url.replace("\\/","/") save_file(the_url,big_path+title+".mp4") urls=["https://www.p***hub.com/view_video.php?viewkey=ph592ef8731630a",] print len(urls), print " videos to download..." count=0 for url in urls: print count count+=1 download_the_av(url) print "All done"

import urllib2 import urllib import datetime import re import os.path import requests def save_file(this_download_url,path): print"- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - " time1=datetime.datetime.now() print str(time1)[:-7], if (os.path.isfile(path)): file_size=os.path.getsize(path)/1024/1024 print "File "+path+" ("+ str(file_size)+"Mb) already exists." return else: print "Downloading "+path+"..." r = requests.get(this_download_url,stream=True) with open(path.encode('utf-8'), "wb") as code: code.write(r.content) time2=datetime.datetime.now() print str(time2)[:-7], print path+" Done." use_time=time2-time1 print "Time used: "+str(use_time)[:-7]+", ", file_size=os.path.getsize(path)/1024/1024 print "File size: "+str(file_size)+" MB, Speed: "+str(file_size/(use_time.total_seconds()))[:4]+"MB/s" def download_url(website_url): fuckyou_header= {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'} req = urllib2.Request(website_url,headers=fuckyou_header) content = urllib2.urlopen(req).read() while len(content)

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180118A0TN1T00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券