介绍性标签:python \x{##**$$}\x{e76f}\x{e76f}{##**$$}##**#.##**$#.
用过的语言:Python3.x
用过的模块:urlopen \
状态:尚未解决
对问题的描述:
我有网址:
http://mapy.cz/#mm=TTtP@x=133054720@y=135947904@z=13它(在web浏览器中)将我重定向到另一个网址:
https://mapy.cz/zakladni?x=14.412346408814274&y=50.08612581835152&z=13我想从路径获得参数x和y。
x = 14.412346408814274
y = 50.08612581835152(地理坐标以十进制为单位)
当我使用:
from urllib.request import urlopen
url = "http://mapy.cz/#mm=TTtP@x=133168128@y=133141248@z=13"
print(urlopen(url).url)它会回报我:
https://mapy.cz/当我使用:
with urlopen(url) as conn:
newUrl = conn.geturl()
print (newUrl)它会回报我:
https://mapy.cz/ 当我使用:
with urlopen(url) as conn:
print (conn.info())它会回报我:
Server: nginx
Date: Sun, 03 Jun 2018 23:24:31 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: close
Cache-Control: max-age=0
Expires: Sun, 03 Jun 2018 23:24:31 GMT
Strict-Transport-Security: max-age=31536000当我使用:
with urlopen(url) as conn:
print (conn.__dict__)它会回报我:
{'fp': <_io.BufferedReader name=1404>, 'debuglevel': 0, '_method': 'GET', 'headers': <http.client.HTTPMessage object at 0x000000000DD48518>, 'msg': 'OK', 'version': 11, 'status': 200, 'reason': 'OK', 'chunked': True, 'chunk_left': None, 'length': None, 'will_close': True, 'code': 200, 'url': 'https://mapy.cz/'}没有提到斜杠后面的参数/路径。既不是原始的网址也不是下面的网址。
当我使用来自What is the quickest way to HTTP GET in Python?的代码时
import urllib.request
contents = urllib.request.urlopen("url").read()它会回报我:
'raw html...'我不想打开/下载html并从html中挖掘这些参数。
发布于 2018-06-04 01:23:11
如果您使用requests (这是您应该使用的),下面是您的特定URL的解决方案:
import requests
URL = 'http://mapy.cz/#mm=TTtP@x=133054720@y=135947904@z=13'
response = requests.get(URL)
if response.history:
print("Request was redirected")
for resp in response.history:
print((resp.status_code, resp.url,))
# getting x, y and z
parts = resp.url.split('@')
x = parts[1].split('=')[1]
y = parts[2].split('=')[1]
z = parts[3].split('=')[1]
print("X {} Y {} Z {}".format(x, y, z))
print("Final destination:")
print((response.status_code, response.url,))
else:
print("Request was not redirected")https://stackoverflow.com/questions/50672383
复制相似问题