Python 如何从字符串中提取 URL 链接

问题来源于 xpath 爬虫,我通过 requests 提取到的 HTML 内容为字符串,不是 json 格式,所以需要提取字符串中的 URL,但是这比 json 数据难处理多了。为此在Google上找到了方法。

What’s the cleanest way to extract URLs from a string using Python?

https://stackoverflow.com/questions/520031/whats-the-cleanest-way-to-extract-urls-from-a-string-using-python/44936558#44936558?newreg=a1ad42438aea44d08f387154dbb6891d

由于提取到的超链接里面既有图片,也有文本(这是由urlextract.py 文件决定的,具体的可以参考GitHub网页 https://github.com/lipoja/URLExtract),我只需要文本的链接,所以需要过滤数据。
Python判断一个字符串是否包含子串的几种方法

https://blog.csdn.net/yl2isoft/article/details/52079960

def get_url():

    with codecs.open('../xinhuanet/汽车_新闻.txt', 'a') as file:
        response = requests.get(homepage, proxies=proxies, headers=headers, params=data)
        print(response.status_code)  # 200
        html = etree.HTML(response.content)
        print(tostring(html).decode())  # 找不到想要的内容
        extractor = URLExtract()
        urls = extractor.find_urls(tostring(html).decode(), only_unique=True)
        # print(urls)
        pc_url = []
        for u in urls:
            flag = ".htm" in u
            if flag is True:
                pc_url.append(u)
                file.writelines(u)
                file.writelines('\n')
    print(pc_url)
    return pc_url
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值