Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.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-在链接中打印单词的正则表达式_Python_Regex_Dictionary_Jupyter Notebook - Fatal编程技术网

Python-在链接中打印单词的正则表达式

Python-在链接中打印单词的正则表达式,python,regex,dictionary,jupyter-notebook,Python,Regex,Dictionary,Jupyter Notebook,我正在使用Jupyter Notebook使用reg ex获取docid=PE209374738作为我的输出。它当前以以下格式存储在字典中: {'Url':'https://backtoschool.com/document.php?docid=PE209374738&datasource=PHE&vid=3326&referrer=api“} 这是我的代码: results= xmldoc.getElementsByTagName("result") dict= {} for a in res

我正在使用Jupyter Notebook使用reg ex获取docid=PE209374738作为我的输出。它当前以以下格式存储在字典中:
{'Url':'https://backtoschool.com/document.php?docid=PE209374738&datasource=PHE&vid=3326&referrer=api“}

这是我的代码:

results= xmldoc.getElementsByTagName("result")
dict= {}
for a in results:
    url= 'Url'
    dict[url] = a.getElementsByTagName("url")[0].childNodes[0].nodeValue
    docid= re.search(r'\?(.*?)&')

有人对如何打印该id有什么建议吗?

标准库已经有了正确解析URL的方法,不需要正则表达式

在Python 3中:

from urllib.parse import urlparse, parse_qs

url = 'https://backtoschool.com/document.php?docid=PE209374738&datasource=PHE&vid=3326&referrer=api'
print(parse_qs(urlparse(url).query)['docid'][0])  # PE209374738
在Python 2中,第一行是:

from urlparse import urlparse, parse_qs

标准库已经有了正确解析URL的方法,不需要正则表达式

在Python 3中:

from urllib.parse import urlparse, parse_qs

url = 'https://backtoschool.com/document.php?docid=PE209374738&datasource=PHE&vid=3326&referrer=api'
print(parse_qs(urlparse(url).query)['docid'][0])  # PE209374738
在Python 2中,第一行是:

from urlparse import urlparse, parse_qs

@alex hall是正确的,您可能最好使用适当的URL解析器来解析它

也就是说,您最初的问题是关于如何使用regexp,所以这里是解决方案(您几乎已经找到了):


这将打印所需的
PE209374738

@alex hall如果正确,您可能最好使用适当的URL解析器解析它

也就是说,您最初的问题是关于如何使用regexp,所以这里是解决方案(您几乎已经找到了):

这将打印所需的
PE209374738