Python WORK 03 -- 提取HTML中的图片URL

Python WORK 03 – 提取HTML中的图片URL

def main():
    input_file = '0503.html'
    output_file = '0503.txt'
    html_lines = get_html_lines(input_file)
    image_urls = extract_image_urls(html_lines)
    show_results(image_urls)
    save_results(output_file, image_urls)


def get_html_lines(path):
    f = open(path, "r", encoding='gbk')
    Is =f.readlines()
    f.close()
    return Is

def extract_image_urls(html_list):
    urls = []
    for line in html_list:
        if 'img' in line:
            url = line.split('src=')[-1].split('"')[1]
            if 'http' in url:
                urls.append(url)
    return urls

def show_results(urls):
    count = 0
    for url in urls:
        print('第{:2}个URL:{}'.format(count, url))
        count+=1

def save_results(path, urls):
    f = open(path, "w")
    for url in urls:
        f.write(url + "\n")
    f.close()

main()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值