Python文本处理

提取URL地址

提取URL地址详细操作教程
通过使用正则表达式从文本文件实现URL提取。表达式在文本与模式匹配的任何位置获取文本。 只有re模块用于此目的。
我们可以将输入文件包含一些URL并通过以下程序处理它以提取URL。 findall()函数用于查找与正则表达式匹配的所有实例。
输入的文本文件
显示的是下面的输入文件。 其中包含几个URL。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-23
Now a days you can learn almost anything by just visiting http://www.google.com. But if you are completely new to computers or internet then first you need to leanr those fundamentals. Next
you can visit a good e-learning site like - https://www.lidihuo.com to learn further on a variety of subjects.
现在,当获取上述输入文件并通过以下程序处理它时,我们得到所需的输出,也就是从文件中提取出来URL地址。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-23
import re
with open("path\url_example.txt") as file:
        for line in file:
            urls = re.findall('https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+', line)
            print(urls)
执行上面示例代码,得到以下结果 -
['http://www.google.com.']
['https://www.lidihuo.com']
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4