Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python从文本中提取url?_Python - Fatal编程技术网

如何使用python从文本中提取url?

如何使用python从文本中提取url?,python,Python,我想在我的文件文本文件中提取带有http或https或不带http的rvry链接 我厌倦了很多代码,但没有和我一起工作 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) 但这是以ht

我想在我的文件文本文件中提取带有http或https或不带http的rvry链接

我厌倦了很多代码,但没有和我一起工作

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://或https://开头的,但我的链接不是以开头的。或如果您有不同的协议,则可以使用此正则表达式:

(.*:\/\/)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#()?&//=]*)

这回答了你的问题吗?你能给一些你的网址的例子吗?