我有一些Dot Net代码可以解析并从URL字符串中检索一个值。
但是,我想执行相同的功能,但现在使用python代码。
Dot Net代码段如下:
An uncaught Exception occurred on "main" thread.
java.lang.RuntimeException: Unable to start activity ComponentInfo{org.nativescript.drawertest/com.tns.NativeScriptActivity}: com.tns.NativeScriptException:
Calling js method onCreate failed
Error: Building UI from XML. @file:///app/main-page.xml:20:5
> Module 'ui/rad-side-drawer' not found for element 'RadSideDrawer'.
> com.tns.NativeScriptException: Failed to find module: "ui/rad-side-drawer", relative to: app/tns_modules/
com.tns.Module.resolvePathHelper(Module.java:159)
com.tns.Module.resolvePath(Module.java:60)
com.tns.Runtime.callJSMethodNative(Native Method)
com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1022)
com.tns.Runtime.callJSMethodImpl(Runtime.java:907)
com.tns.Runtime.callJSMethod(Runtime.java:895)
com.tns.Runtime.callJSMethod(Runtime.java:879)
com.tns.Runtime.callJSMethod(Runtime.java:871)
com.tns.NativeScriptActivity.onCreate(android.app.Activity.java)
android.app.Activity.performCreate(
提前致谢
答案 0 :(得分:2)
如果可用,最好使用std lib函数。 Python 3有urllib.parse
(如果你还在Py2上,它是urlparse
)。使用此模块的urlparse
方法提取网址的查询部分('?'之后的内容)。然后parse_qs
会将此查询转换为key:list
值的字典 - 值是用于处理具有重复键的查询字符串的列表。
url = 'http://www.somesite.blah/page?id=12345&attr=good&attr=bad&attr=ugly'
try:
from urllib.parse import urlparse, parse_qs
except ImportError:
# still using Python 2? time to move up
from urlparse import urlparse, parse_qs
parts = urlparse(url)
print(parts)
query_dict = parse_qs(parts.query)
print(query_dict)
print(query_dict['id'][0])
打印:
ParseResult(scheme='http', netloc='www.somesite.blah', path='/page', params='',
query='id=12345&attr=good&attr=bad&attr=ugly', fragment='')
{'attr': ['good', 'bad', 'ugly'], 'id': ['12345']}
12345
答案 1 :(得分:0)
first,last = query_String.split(' =')