Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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
提取URL';从XML到Python中的_Python_Regex - Fatal编程技术网

提取URL';从XML到Python中的

提取URL';从XML到Python中的,python,regex,Python,Regex,我读了这篇关于从字符串中提取url的文章。 非常好,我从一个包含 还是这个 re.findall(r'(https?://\S+\Z")', s) 但事实并非如此 有人能帮我一下,告诉我如何省略结尾的双引号吗 顺便说一句,https的“s”后面的问号表示“s”可以出现或不能出现。我说的对吗?您希望双引号显示为前瞻: re.findall(r'(https?://\S+)(?=\")', s) 这样他们就不会出现在比赛中。另外,是?表示该字符是可选的 参见此处示例:谢谢。我刚读到这个 并且检查

我读了这篇关于从字符串中提取url的文章。 非常好,我从一个包含

还是这个

re.findall(r'(https?://\S+\Z")', s)
但事实并非如此

有人能帮我一下,告诉我如何省略结尾的双引号吗


顺便说一句,https的“s”后面的问号表示“s”可以出现或不能出现。我说的对吗?

您希望双引号显示为前瞻:

re.findall(r'(https?://\S+)(?=\")', s)
这样他们就不会出现在比赛中。另外,是
表示该字符是可选的

参见此处示例:

谢谢。我刚读到这个

并且检查了这个,它也在工作

re.findall(r'"(https?://\S+)"', urls) 

我曾经通过以下代码从文本中提取URL:

url_rgx = re.compile(ur'(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?\xab\xbb\u201c\u201d\u2018\u2019]))')
# convert string to lower case
text = text.lower()
matches = re.findall(url_rgx, text)
# patch the 'http://' part if it is missed
urls = ['http://%s'%url[0] if not url[0].startswith('http') else url[0] for url in matches]
print urls
(5)以下以下以下:(:(5)以下以下:(:(5)以下以下:(5)以下:(5)以下以下:(5)以下以下:(:(5)以下:(:(5)以下以下:(5)以下:(5)以下(5)以下(5)代码>url)url)rgx=再。编译(5)再。编译(5)编译(U)的(5)的(5)以下以下(5)以下以下(5)的(5)网址:网址:::::(10.0,3)3)3)上述上述上述上述网址(10)的网址(10)url)的网址(10)url)rgx-rgx=10.10.10.10.10.10.10.10.10.10.10.url)的网址(该方方方方方方方方方方方(该该次次次次次次次次次次次次次次次次次次次次次次次((该方方方方方方方方方)的(10)的主要主要主要主要主要主要的(u201d\u2018\u2019])) #将字符串转换为小写 text=text.lower() matches=re.findall(url\u rgx,文本) #如果缺少“http://”部分,请对其进行修补 url=['http://%s'%url[0],如果不是url[0]。使用('http')启动其他url[0],用于匹配中的url] 打印URL
它工作得很好!

您已经在使用字符类(尽管是速记版本)。我可能建议稍微修改字符类,这样您就不需要预先查看。只需将引号作为字符类的一部分添加即可:

re.findall(r'(https?://[^\s"]+)', s)

这仍然表示“一个或多个字符不是空白”,但添加了不包含双引号的内容。因此,整体表达式是“一个或多个字符不是空白,也不是双引号”。“

永远不要用正则表达式解析html你也应该阅读线程如果你使用像BeautifulSoup这样的html解析器,这个问题将比使用正则表达式更容易。他并不是真的在解析html。。。他正在从文档中挖掘链接。这是一个完全可以接受的正则表达式用法。是的,我正在解析一个XML,抱歉,但我在这个例子中遇到了与HTMLyes相同的问题,但是如果文本中有一个URL带有其他字符,例如“>asdf”,它将返回:[这是错误的!
url_rgx = re.compile(ur'(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?\xab\xbb\u201c\u201d\u2018\u2019]))')
# convert string to lower case
text = text.lower()
matches = re.findall(url_rgx, text)
# patch the 'http://' part if it is missed
urls = ['http://%s'%url[0] if not url[0].startswith('http') else url[0] for url in matches]
print urls
re.findall(r'(https?://[^\s"]+)', s)
>>>from lxml import html
>>>ht = html.fromstring(s)
>>>ht.xpath('//a/@href')
['http://www.blabla.com/blah', 'http://www.blabla.com']