Back
Featured image of post python3 | requests的简单应用1

python3 | requests的简单应用1

下载已知url列表的视频文件,获取主要地区天气信息,以指定格式写入文件

下载视频

读取文件中的 URL 和 标题,根据URL下载视频到本地(以标题作为文件名)。

#t1.txt 文件的样子
模仿,https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0300f570000bvbmace0gvch7lo53oog&ratio=720p&line=0
卡特,https://stream7.iqilu.com/10339/upload_transcode/202002/18/20200218114723HDu3hhxqIT.mp4
罗斯,https://stream7.iqilu.com/10339/upload_transcode/202002/18/20200218093206z8V1JuPlpe.mp4
# 下载视频示例
import os
import requests
# 对拿到的下载名称和下载链接文件txt做处理,提取出来。
def data(file_path):
    list = []
    with open(file_path,mode='r',encoding='utf-8') as f:
        for line in f:
            video_name, url = line.strip().split(',')
            list.append([video_name,url])
    return list

# 判断下载好的文件存入的地方是否存在想要的目录,不存在就新建一个
def video_path(str):
    if not  os.path.exists(str):
        os.mkdir(str)
    else:
        pass

# 对传入的list清单进行下载
def download(list):
    num = 0
    for i in list:# i是每个视频的名字和链接
        url = i[1] #
        res = requests.get( #当前视频,headers有些网站不带这个不让获取
            url=url,
            headers={
                "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 FS"
            }
        )
        name = i[0]
        video_path("E:\\fuckpy\\files\\video") # 检测待存储文件目录是否存在
        with open("files/video/{}.mp4".format(name),mode="wb") as video_obj:    #新建文件
            video_obj.write(res.content)    #写入
        print("新增1个")
        num = num + 1
    print(f"本次共增加{num}个视频")

if __name__ == '__main__':
    path = 'files/t1.txt'
    video= data(path)
    download(video)

获取天气信息,以指定格式写入文件中

获取地址:url=“http://www.weather.com.cn/data/ks/101010100.html

格式要求:

 1.    每个城市的天气占一行

 2.    每行的格式为:city-北京,cityid-101010100,temp-18...
# 观察url猜规律,生成城市代码的list
def get_city_code():
    city_code = []
    for i in range(1,35):
        if i in [1,2,3,4]:
            sq = "101"+"0"+str(i)+"0100"
            city_code.append(sq)
        elif i in [5,6,7,8,9]:
            sq = "101" + "0" + str(i) + "0101"
            city_code.append(sq)
        else:
            sq = "101" + str(i) + "0101"
            city_code.append(sq)
    return city_code

# 清洗城市信息的dict
def clear_info(dict):
    last = []
    # 将字典的key和values提出来作为2个list
    key = list(dict.keys())
    value = list(dict.values())
    for i in range(0, len(dict)):
        # 进行字符串拼接,得到:"city-北京" 这样的子元素
        last.append(key[i] + "-" + value[i])
    #last作为一个list已经非常接近要求的格式,但是得去掉外面的[]和每个子字符串的引号
    reslut = ",".join(last)
    return reslut
# 城市的信息作为一个dict传入到这里,尝试添加到文件city-info.txt
def add_info(dict):
    # 加入之前清洗信息,符合要求的格式
    text = clear_info(dict)
    with open('files/city-info.txt',mode='a',encoding='utf-8') as f:
        f.write(text)
        f.write('\n')

# 包放到函数内部,不知道是不是不规范....
def test_code(code):
    import requests
    for i in code:
        res = requests.get(url="http://www.weather.com.cn/data/ks/{}.html".format(i))
        res.encoding = 'utf-8'
        try:
            weather_dict = res.json()
            # 因为自带一层{}不喜欢,拿掉了,keep real
            real = weather_dict['weatherinfo'] 
            # 添加进文件
            add_info(real)
            # 提示已经加入成功
            print("已经加入编号:{},{}的城市信息".format(i,real['city']))
        except:
            print("something error")
if __name__ == '__main__':

    city_code = get_city_code()
    test_code(city_code)

扩展

Python’s Requests Library (Guide) 主要看的这个

Requests: HTTP for Humans™ 貌似算官方推荐

Requests: 让 HTTP 服务人类 上面内容的中文版本

Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy