如何用Python re模块精准匹配{"id":}至"address1":间的目标短语
问题描述
尝试用Python的re模块匹配特定模式的短语,代码如下:
import requests import re mlocations=requests.get("https://m.happyfresh.id/supplier/tip-top-hfc?tracking_source=backlink_storehome").text data = re.findall(r'(?={"id":)(.*?)(?="address1":)', mlocations)
mlocations的片段内容:
~50%","seo_details":null,"store_categories_name":["Daily Basic Needs","Supermarket"]}},{"id":11649,"name":"HappyFresh Supermarket Depok","address1":"Jl. Gas Alam Raya No.90, RW.5, Curug, Kec. Cimanggis, Kota Depok, Jawa Barat","city":null,"zipcode":"16454","phone":"","lat":-6.38367246279211,"lon":106.876803265144,"slug":"happyfresh-supermarket-depok","photo":null,"state_name":"Depok","supplier":{"id":3468,"name":"HappyFresh Supermarket - ID","slug":"tip-top-hfc","supplier_type":"warehouse","instant_delivery":true,"delivery_time":"","delivery_price":"","brand_store_image":null,"photo":"https://cdn.happyfresh.com/spree/suppliers/photos/83817f71b5105cea577fdc3b7269004e97e27dcc-medium.jpg?1636468094","square_background":"#ffffff","square_photo":"https://cdn.happyfresh.com/spree/suppliers/square_photos/01c5f038b42d965441eb4ef55793ebb3e16d4213-medium.png?1636468095","store_photo":null,"background_square_photo":{"mini_url":"https://cdn.happyfresh.com/spree/suppliers/background_square_photos/ee855daf2c26df788660693c7c7c4f590fa6b24d-mini.png?1625202943","small_url":"https://cdn.happyfresh.com/spree/suppliers/background_square_photos/068a23faf0395d66a99d3322c52d4b27803ed9c3-small.png?1625202943","medium_url":"https://cdn.happyfresh.com/spree/suppliers/background_square_photos/8e33766f8f2957f2f7df1c6190c4edb700438e6f-medium.png?1625202943","large_url":"https://cdn.happyfresh.com/spree/suppliers/background_square_photos/0b790dd195f28fb2788cc4567e72b1c7e260505e-large.png?1625202943"},"display_promotion_label":"Diskon ~50%","seo_details":null,"store_categories_name":["Daily Basic Needs","Supermarket"]}},{"id":6501,"name":"HappyFresh Supermarket Cilandak","address1":"Pergudangan Perum Peruri, Gudang 7, Jl Lebak Bulus I, Cilandak, Jakarta Selatan ","city":"Jakarta","zipcode":"12430","phone":"","lat":-6.29639070332991,"lon":106.79448776706,"slug":"happyfresh-supermarket-cilandak","photo":null,"state_name":"Jakarta Selatan","supplier":{"id":3468,"name":"HappyFresh Supermarket - ID","slug":"tip-top-
预期返回结果:
11649,"name":"HappyFresh Supermarket Depok" 6501,"name":"HappyFresh Supermarket Cilandak"
但实际返回了{"id":}到"address1":之间的所有内容,需要修正正则以获取目标短语。
解决方案
方法一:修正正则表达式
原正则的.*?会模糊匹配到多余内容,改用精准匹配规则锁定id和name部分:
data = re.findall(r'(?={"id":)(\d+,"name":"[^"]+)(?="address1":)', mlocations)
正则规则说明:
\d+:匹配纯数字的id值"name":"[^"]+":匹配"name":"xxx"结构,[^"]+表示只取双引号内的名称内容,避免匹配到后续字段
方法二:使用JSON解析(更推荐)
目标内容本质是JSON结构,用正则处理容易因格式变化出错,直接用Python内置的json模块解析更稳定:
import requests import json import re mlocations = requests.get("https://m.happyfresh.id/supplier/tip-top-hfc?tracking_source=backlink_storehome").text # 先提取页面中的JSON数组(根据实际页面结构调整匹配规则) json_match = re.search(r'(\[.*?\])', mlocations) if json_match: stores = json.loads(json_match.group(1)) # 提取需要的内容 result = [f'{store["id"]},"name":"{store["name"]}"' for store in stores] for item in result: print(item)
这种方法不受JSON格式的空格、换行等细节影响,匹配结果更可靠。
内容的提问来源于stack exchange,提问作者Hal
相关产品推荐
相关产品推荐

