av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

python正則怎么提取域名

瀏覽:138日期:2022-06-28 09:49:42

問題描述

<script type='application/ld+json'>{ '@context': 'http://schema.org', '@type': 'SaleEvent', 'name': '10% Off First Orders', 'url': 'https://www.myvouchercodes.co.uk/coggles', 'image': 'https://mvp.tribesgds.com/dyn/oh/Ow/ohOwXIWglMg/_/mQR5xLX5go8/m0Ys/coggles-logo.png', 'startDate': '2017-02-17', 'endDate': '2017-12-31', 'location': {'@type': 'Place','name': 'Coggles','url': 'coggles.co.uk','address': 'Coggles' }, 'description': 'Get the top branded fashion items from Coggles at discounted prices. Apply this code and enjoy savings on your purchase.', 'eventStatus': 'EventScheduled'}</script>

怎么用python正則從這段腳本中提取coggles.co.uk域名呢,望各路高手指點顯示下身手...

問題解答

回答1:

正則實現的話只要保證你的標定/特征是唯一的就好。但是'url'這個標志又不是唯一的。這個時候@prolifes的方法是很好的。

如果一定要正則實現呢,要用到零寬斷言(zero-width assertions),當然這個詞的翻譯比較直,帶來很多誤解。它其實意思是指定位置的匹配,位置的寬度就是0嘛。

這里我們可以看到我們所需的這個'url'在'location'里面,可以以此為位置信息。

代碼如下:

re.search(’(?<=location).+?'url': '([^']+)'’, string, re.DOTALL).group(1)

稍微解釋一下,(?<=location)這個地方就是指前面得有location。后面有的話這樣寫:(?=location)re.DOTALL這個是必須的,因為這些字符串已經跨行了。他的作用是將.的字符串匹配范圍擴大,包含換行符。'([^']+)'這個地方是我的習慣,[^']意指所有非'的字符,這就匹配了雙引號中所有的字符串。

回答2:

這是一段挺標準的json,粗暴一點,直接轉換成json

import jsonstr = ’’’<script type='application/ld+json'>{ '@context': 'http://schema.org', '@type': 'SaleEvent', 'name': '10% Off First Orders', 'url': 'https://www.myvouchercodes.co.uk/coggles', 'image': 'https://mvp.tribesgds.com/dyn/oh/Ow/ohOwXIWglMg/_/mQR5xLX5go8/m0Ys/coggles-logo.png', 'startDate': '2017-02-17', 'endDate': '2017-12-31', 'location': {'@type': 'Place','name': 'Coggles','url': 'coggles.co.uk','address': 'Coggles' }, 'description': 'Get the top branded fashion items from Coggles at discounted prices. Apply this code and enjoy savings on your purchase.', 'eventStatus': 'EventScheduled'}</script>’’’d = json.loads(re.search(’({[sS]*})’, str).group(1))print d[’location’][’url’]

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 夜夜爽99久久国产综合精品女不卡 | 久久首页 | 伊人久久免费 | 精品一区二区三区四区五区 | 日韩欧美视频 | 中文字幕日韩一区 | 国产一区二区免费在线 | 免费一级片| 精品无码久久久久久久动漫 | 亚洲精品视频一区 | 欧美久 | 台湾佬久久| 精品国产99 | 激情欧美一区二区三区 | 99综合| 黄色片在线观看网址 | 老司机67194精品线观看 | 欧美成人精品一区二区三区 | 国产日韩欧美一区二区 | 欧美精品一区二区免费 | 成人一区在线观看 | 国产精品日韩一区 | 在线观看第一页 | 亚洲精品在 | 亚洲一区二区视频在线播放 | 先锋资源站 | 99精品网 | 91在线精品一区二区 | 日本一区二区三区精品视频 | 少妇一级淫片免费播放 | 一级片av| 一区二区视频 | 91秦先生艺校小琴 | 欧美一级片免费看 | 亚洲欧美在线视频 | 国产区视频在线观看 | 成年视频在线观看福利资源 | 91在线色视频 | 国产精品成人一区二区 | 毛片一级片 | 国产99久久精品一区二区永久免费 |