Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.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中包含的URL?_Python_Html_Parsing_Url_Urlencode - Fatal编程技术网

如何使用Python可靠地提取URL中包含的URL?

如何使用Python可靠地提取URL中包含的URL?,python,html,parsing,url,urlencode,Python,Html,Parsing,Url,Urlencode,许多搜索引擎通过将结果的URL添加到查询字符串来跟踪单击的URL,查询字符串可以采用如下格式:http://www.example.com/result?track=http://www.stackoverflow.com/questions/ask 在上面的示例中,结果URL是查询字符串的一部分,但在某些情况下,其形式为http://www.example.com/http://www.stackoverflow.com/questions/ask或使用URL编码 我首先尝试的方法是拆分sea

许多搜索引擎通过将结果的URL添加到查询字符串来跟踪单击的URL,查询字符串可以采用如下格式:
http://www.example.com/result?track=http://www.stackoverflow.com/questions/ask

在上面的示例中,结果URL是查询字符串的一部分,但在某些情况下,其形式为
http://www.example.com/http://www.stackoverflow.com/questions/ask
或使用URL编码

我首先尝试的方法是拆分
searchengineurl.split(“http://”)
。这方面的一些明显问题:

  • 它将返回结果URL后面的查询字符串的所有部分,而不仅仅是结果URL。这样的URL可能会出现问题:
    http://www.example.com/result?track=http://www.stackoverflow.com/questions/ask&showauthor=False&display=None
  • 它不区分搜索引擎跟踪URL的查询字符串和结果URL的查询字符串的任何其他部分。这样的URL可能会出现问题:
    http://www.example.com/result?track=http://www.stackoverflow.com/questions/ask?showauthor=False&display=None
  • 如果在结果URL中输入“http://”,则失败

Python中提取其他URL中包含的URL的最可靠、通用和非黑客的方法是什么?

我会尝试使用
urlparse.urlparse
它可能会帮助您完成大部分工作,您需要做一些额外的工作。

我不太了解Python,但是我会使用正则表达式来获取查询字符串的部分(key=value),比如

(?:\?|&)[^=]+=([^&]*)
它捕获了“价值”部分。然后,我会解码它们,并对照另一个模式(可能是另一个正则表达式)检查它们,看看哪一个看起来像URL。我只需要检查第一部分,然后取整个值。这样,您的模式就不必考虑每一种可能的URL类型(而且可能它们没有将URL与单个值字段中的其他内容组合在一起)。无论是否指定协议,这都应该起作用(由您的模式决定URL的外观)

至于第二种类型的URL。。。我不认为有一个非黑客的方式来解析它。您可以对整个URL进行URL解码,然后查找http://(或https://,和/或您可能遇到的任何其他协议)的第二个实例。您必须决定任何查询字符串是“您的”URL还是跟踪器URL的一部分。您还可以解码URL并尝试匹配编码值。无论哪种方式都会很混乱,如果他们不包括协议,情况会更糟!如果你正在使用一组特定的格式,你可以为它们制定好的规则。。。但是如果你不得不处理他们向你扔的东西。。。我认为没有可靠的方法来处理第二种类型的嵌入。

这对我来说很有效

from urlparse import urlparse
from urllib import unquote

urls =["http://www.example.com/http://www.stackoverflow.com/questions/ask", 
"http://www.example.com/result?track=http://www.stackoverflow.com/questions/ask&showauthor=False&display=None", 
"http://www.example.com/result?track=http://www.stackoverflow.com/questions/ask?showauthor=False&display=None",
"http://www.example.com/result?track=http%3A//www.stackoverflow.com/questions/ask%3Fshowauthor%3DFalse%26display%3DNonee"]

def clean(url):
    path = urlparse(url).path
    index = path.find("http")
    if not index == -1:
        return path[index:]
    else:
        query = urlparse(url).query
        index = query.index("http")
        query = query[index:]
        index_questionmark = query.find("?")
        index_ampersand = query.find("&")
        if index_questionmark == -1 or index_questionmark > index_ampersand:
            return unquote(query[:index_ampersand])
        else:
            return unquote(query)

for url in urls:
    print clean(url)

> http://www.stackoverflow.com/questions/ask
> http://www.stackoverflow.com/questions/ask
> http://www.stackoverflow.com/questions/ask?showauthor=False&display=None
> http://www.stackoverflow.com/questions/ask?showauthor=False&display=None

urlparse.parse\u qs
会有帮助吗?还是你在寻找更强壮的东西?