Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.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
如何使用lxml在Python中获取经过解析的HTML页面的当前url?_Python_Parsing_Url_Lxml - Fatal编程技术网

如何使用lxml在Python中获取经过解析的HTML页面的当前url?

如何使用lxml在Python中获取经过解析的HTML页面的当前url?,python,parsing,url,lxml,Python,Parsing,Url,Lxml,在Python中,我正在解析各种URL,以便在返回文档的主体中找到一些元素。我使用lxml来实现这一点,如下所示: import lxml.html as html url = 'http://www.linktowebsite.com' data = html.parse(url) for d in data.xpath('body'): code code code 然而,一些URL重定向到不同的页面,我想知道重定向后的当前URL。我在lxml的文档中没有找到任何与此相关的内容

在Python中,我正在解析各种URL,以便在返回文档的主体中找到一些元素。我使用lxml来实现这一点,如下所示:

import lxml.html as html

url = 'http://www.linktowebsite.com'
data = html.parse(url)

for d in data.xpath('body'):
    code code code
然而,一些URL重定向到不同的页面,我想知道重定向后的当前URL。我在lxml的文档中没有找到任何与此相关的内容


如何找到已解析/重定向页面的当前URL?

使用
data.docinfo.URL

例如:

In [22]: data = html.parse('http://httpbin.org/redirect/2')

In [23]: data.docinfo.URL
Out[23]: u'http://httpbin.org/get'
看看这里