urllib实战2--urllib基础urlretrieve()、urlcleanup()、info()、getcode()、geturl()的应用

一:urllib基础

     要系统的学习urllib模块,就要从基础开始,下面会为大家实战讲解urlretrieve()、urlcleanup()、info()、getcode()、geturl()等。

二:实战讲解

    1:urlretrieve()的应用,可以将网站网址直接爬取到本地中

       格式:  request.urlretrieve(url,filename)     url为 要爬取的网站的地址,filename为本地的名。


此时打开文件

打开015y.html这个文件.


 2:urlcleanup()的应用,可以将urlretrieve()中的缓存清理掉:


3:info() 可以将当前的基本环境信息显示出来:


4:getcode()获取当前的网页的状态码,geturl()获取当前的网页的网址。

     200状态码表示网页正常,403表示不正常。


三。综上所述,所有代码为:

>>> from urllib import request
>>> request.urlretrieve("http://www.hellobi.com",filename="G:/BaiduDownload/python网络爬虫/WODE/015y.html")
>>> request.urlcleanup()
>>> request.urlopen("http://www.hellobi.com")
>>> file=request.urlopen("http://www.hellobi.com")
>>> file.info()
>>> file.getcode()
>>> file.geturl()

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import requests import urllib.request import os def quest_find(quest_url, awme_id): params = {"id": awme_id} respon = requests.get(quest_url, params=params).json() return respon["data"], respon["code"] def re_down(url,filename): try: urllib.request.urlretrieve(url,filename) except urllib.error.ContentTooShortError: print ('Network conditions is not good. Reloading...') re_down(url,filename) # 获取视频URL,并下载 if __name__ == '__main__': quest_url = "http://discover-rpc.cmm-crawler-intranet.k8s.limayao.com/play_url" save_path = "/home/algodev/sujunbin/whisper/test_model/video%s" %time if not os.path.exists(save_path): os.mkdir(save_path) awme_ids = ['7119114587735100687'] with open('id_time.txt','r') as file: for line in file.readlines(): line = line.split() id = line[0] time1 = int(line[1]) if time1<10000: time ='<10s' elif 10000<=time1<20000: time='10-20s' elif 20000<=time1<30000: time='20-30s' elif 30000<=time1<40000: time='30-40s' elif 40000<=time1<50000: time='40-50s' elif 50000<=time1<60000: time='50-60s' elif 60000<=time1<90000: time='60-90s' elif 90000<=time1<120000: time='90-120s' elif 120000<=time1<180000: time='120-180s' elif time1>=180000: time='>180s' save_path = "/home/algodev/sujunbin/whisper/test_model/video%s" %time if not os.path.exists(save_path): os.mkdir(save_path) data_json, code = quest_find(quest_url, id) play_url = data_json['play_url'] video_name = id + '.mp4' save_video_path = os.path.join(save_path, video_name) re_down(data_json['play_url'], save_video_path) print(save_video_path) for i in range(len(awme_ids)): data_json, code = quest_find(quest_url, awme_ids[i]) play_url = data_json['play_url'] video_name = awme_ids[i] + '.mp4' save_video_path = os.path.join(save_path, video_name) urllib.request.urlretrieve(data_json['play_url'], save_video_path) print(save_video_path) print("done!")这段代码有什么问题
06-13

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值