例子
我们可以获取一个包含一些 URL 的输入文件,并通过以下程序对其进行处理以提取 URL。这findall()函数用于查找与正则表达式匹配的所有实例。
输入输出文件
显示的是下面的输入文件。其中包含 teo URL。
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.jc2182.com to learn further on a variety of subjects.
现在,当我们获取上述输入文件并通过以下程序对其进行处理时,我们将获得所需的输出,其中仅给出从文件中提取的 URL。
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.jc2182.com']