使用urllib的Python打开URL比获取打开的网页的更改URL更新

时间:2016-09-28 14:17:58

标签: python url urllib2

我想向Google地图发送一些请求。我打开根据请求更改的URL。我想要找回更改后的网址。 一个例子:

public class ParkingSpace
{
    public Guid Guid { get;}
    public string DescriptionHint { get; private set; }
    public int Id { get; private set; }
    private ParkingSpaceState State { get; set; }
}

要打开的网址(import urllib, urllib2 my_address = '1600 Amphitheatre Parkway Mountain View, CA 94043' data = urllib.urlencode({'output':'csv', 'q':my_address}) req = urllib2.Request('https://www.google.co.uk/maps/place?' + data) res_0 = urllib2.urlopen(req) print res_0.geturl() ):

res_0.geturl()

我想要找回更改后的网址,即:

'https://www.google.co.uk/maps/search/1600+Amphitheatre+Parkway+Mountain+View,+CA+94043/data=!4m2!2m1!4b1?dg=dbrw&newdg=1'

我手动在浏览器中打开了'https://www.google.co.uk/maps/place/1600+Amphitheatre+Pkwy,+Mountain+View,+CA+94043,+USA/@37.4223371,-122.0866079,17z/data=!3m1!4b1!4m5!3m4!1s0x808fba027820e5d9:0x60a90600ff6e7e6e!8m2!3d37.4223329!4d-122.0844192'网址,我得到了上面更改的网址。

我该怎么做?

谢谢!

1 个答案:

答案 0 :(得分:0)

你可以在urllib2

中使用.geturl()方法

示例:

print res_0.geturl()