找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 1524|回复: 0

[代码与实例] python提取网页源代码的图像链接网址为什么失败?

0

主题

0

帖子

0

积分

贫民

积分
0
tangxiaomin1996 发表于 2022-7-19 21:05:01 | 显示全部楼层 |阅读模式
本帖最后由 tangxiaomin1996 于 2022-7-19 21:07 编辑

代码如下,请高手指教
def getHTMLlines(htmlpath):
    f=open(htmlpath,'r',encoding='utf-8')
    ls=f.readlines()
    f.close()
    return ls

def extractImageUrls(htmllist):
    urls=[]
    for line in htmllist:
        if 'a' in line:
            url=line.split('href=')[-1].split('"')[0]

            if 'http' in url:
                urls.append(url)
    return urls

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

def saveResults(filepath,urls):
    f=open(filepath,'w')
    for url in urls:
        f.write(url+'\n')
    f.close()

def main():
    intputfile='baidu3.html'
    outputfile='baidu3-urls.txt'
    htmlLines=getHTMLlines(intputfile)
    imageUrls=extractImageUrls(htmlLines)
    showResults(imageUrls)
    saveResults(outputfile,imageUrls)

main()


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表