我如何使用正则表达式和python提取以电话开头并以'}'结尾的短语
我试图从页面源中提取数据。
<v-list-tile-title v-if="isAdminMethod===true">
<a href="/admin">Admin Panel</a>
</v-list-tile-title>
<v-list-tile-title v-else>
<router-link :to="{ path: i.to }">
{{ i.title}}
</router-link>
</v-list-tile-title>
我想用正则表达式提取所有以phone开头并以}结尾的短语
我尝试过re.findall(r“ ^ phone(。*)} $”,来源)
我想要的是“电话”,“值”:“ + XXXXXXXX”,“链接”:“ tel:+ XXXXXXXX”,“ is_did”:true}
答案 0 :(得分:1)
为此最好使用json而不是正则表达式。 试试吧,
import json
test_str = '{"meta":{"subtitle":"Apartment for Rent in Marina Gate 1, Marina Gate","price":145000,"price_text":"145,000 AED/year","contact_options":{"list":{"phone":{"type":"phone","value":"+XXXXXXXX","link":"tel:+XXXXXXXX","is_did":true},"email":{"type":"email","value":"name@email.com","link":"mailto:name@email.com"}},"details":{"phone":{"type":"phone","value":"+XXXXXXXX","link":"tel:+XXXXXXXX","is_did":true},"sms":{"type":"sms","value":"+XXXXXXXX","link":"sms:+XXXXXXXX"},"email":{"type":"email","value":"name@email.com","link":"mailto:name@email.com"}}},"images_count":11}}'
print test_str
json_str = json.loads(test_str)
print json_str
phone_num = json_str['meta']['contact_options']['list']['phone']
print phone_num
答案 1 :(得分:0)
您可以尝试以下代码(使用<script>
解析re
标签):
import requests
import json
import re
html_text = requests.get('https://www.propertyfinder.ae/en/rent/apartment-for-rent-dubai-dubai-marina-marina-gate-marina-gate-1-6951117.html').text
data = json.loads(re.findall(r'payload\s*:\s*(.*?)\n', html_text)[0])
for d in data['data']:
print(d['meta']['subtitle'])
print(d['meta']['contact_options'])
print('*' * 80)
打印:
Apartment for Rent in Marina Gate 1, Marina Gate
{'list': {'phone': {'type': 'phone', 'value': '+971528347286', 'link': 'tel:+971528347286', 'is_did': True}, 'email': {'type': 'email', 'value': 'ahmad@providentestate.com', 'link': 'mailto:ahmad@providentestate.com'}}, 'details': {'phone': {'type': 'phone', 'value': '+971528347286', 'link': 'tel:+971528347286', 'is_did': True}, 'sms': {'type': 'sms', 'value': '+971581806000', 'link': 'sms:+971581806000'}, 'email': {'type': 'email', 'value': 'ahmad@providentestate.com', 'link': 'mailto:ahmad@providentestate.com'}}}
********************************************************************************
Apartment for Rent in Marina Gate 1, Marina Gate
{'list': {'phone': {'type': 'phone', 'value': '+971525226138', 'link': 'tel:+971525226138', 'is_did': True}, 'email': {'type': 'email', 'value': 'suhail.p@w2realestate.com', 'link': 'mailto:suhail.p@w2realestate.com'}}, 'details': {'phone': {'type': 'phone', 'value': '+971525226138', 'link': 'tel:+971525226138', 'is_did': True}, 'sms': {'type': 'sms', 'value': '+971503940533', 'link': 'sms:+971503940533'}, 'email': {'type': 'email', 'value': 'suhail.p@w2realestate.com', 'link': 'mailto:suhail.p@w2realestate.com'}}}
********************************************************************************
Apartment for Rent in Marina Gate 1, Marina Gate
{'list': {'phone': {'type': 'phone', 'value': '+971528347286', 'link': 'tel:+971528347286', 'is_did': True}, 'email': {'type': 'email', 'value': 'ahmad@providentestate.com', 'link': 'mailto:ahmad@providentestate.com'}}, 'details': {'phone': {'type': 'phone', 'value': '+971528347286', 'link': 'tel:+971528347286', 'is_did': True}, 'sms': {'type': 'sms', 'value': '+971581806000', 'link': 'sms:+971581806000'}, 'email': {'type': 'email', 'value': 'ahmad@providentestate.com', 'link': 'mailto:ahmad@providentestate.com'}}}
********************************************************************************
Apartment for Rent in Marina Gate 1, Marina Gate
{'list': {'phone': {'type': 'phone', 'value': '+971522233791', 'link': 'tel:+971522233791', 'is_did': True}, 'email': {'type': 'email', 'value': 'eddy@exclusive-links.com', 'link': 'mailto:eddy@exclusive-links.com'}}, 'details': {'phone': {'type': 'phone', 'value': '+971522233791', 'link': 'tel:+971522233791', 'is_did': True}, 'sms': {'type': 'sms', 'value': '+971523279984', 'link': 'sms:+971523279984'}, 'email': {'type': 'email', 'value': 'eddy@exclusive-links.com', 'link': 'mailto:eddy@exclusive-links.com'}}}
********************************************************************************
Apartment for Rent in Marina Gate 1, Marina Gate
{'list': {'phone': {'type': 'phone', 'value': '+971565775168', 'link': 'tel:+971565775168', 'is_did': False}, 'email': {'type': 'email', 'value': 'julia@abodeproperty.ae', 'link': 'mailto:julia@abodeproperty.ae'}}, 'details': {'phone': {'type': 'phone', 'value': '+971565775168', 'link': 'tel:+971565775168', 'is_did': False}, 'sms': {'type': 'sms', 'value': '+971565775168', 'link': 'sms:+971565775168'}, 'email': {'type': 'email', 'value': 'julia@abodeproperty.ae', 'link': 'mailto:julia@abodeproperty.ae'}}}
********************************************************************************
注意:有时网站会返回格式错误的HTML代码,因此您需要多次运行脚本,直到脚本成功为止(也许您需要调整regex
-我没有进一步调查)