如何使用Python或beautifulsoup从文件(带有in-script标记)中提取文本
你能帮我做这件小事情吗。我希望使用Beautiful soup(Python)或Python从下面的脚本标记(而不是正文)代码中提取lat和lng值。我是Python新手,博客推荐使用BeautifulSoup进行提取 我想要这两个值lat:21.25335,lng:81.649445 我正在为此使用正则表达式。我的常规表达式“^l([a-t])(:)([0-9])([^,]+)” 检查正则表达式和html文件的此链接- 我用这个正则表达式得到这两个值,但我只想把lat和lng值(数值部分)存储在变量中 下面是我正在使用的python代码如何使用Python或beautifulsoup从文件(带有in-script标记)中提取文本,python,regex,python-2.7,beautifulsoup,Python,Regex,Python 2.7,Beautifulsoup,你能帮我做这件小事情吗。我希望使用Beautiful soup(Python)或Python从下面的脚本标记(而不是正文)代码中提取lat和lng值。我是Python新手,博客推荐使用BeautifulSoup进行提取 我想要这两个值lat:21.25335,lng:81.649445 我正在为此使用正则表达式。我的常规表达式“^l([a-t])(:)([0-9])([^,]+)” 检查正则表达式和html文件的此链接- 我用这个正则表达式得到这两个值,但我只想把lat和lng值(数值部分)存
import re
pattern = re.compile("^[l]([a-t])([a-t])(\:) ([0-9])([^,]+)")
for i, line in enumerate(open('C:\hile_text.html')):
for match in re.finditer(pattern, line):
print 'Found on line %s: %s' % (i+1, match.groups())
输出:
- 在第3218行中找到:('a','t',':','2','1.244791')
- 在第3219行找到:('n','g',':','8','1.643486')
<script type="text/javascript">
window.mapDivId = 'map0Div';
window.map0Div = {
lat: 21.25335,
lng: 81.649445,
zoom: null,
locId: 5897747,
geoId: 297595,
isAttraction: false,
isEatery: true,
isLodging: false,
isNeighborhood: false,
title: "Aman Age Roll & Chicken ",
homeIcon: true,
url: "/Restaurant_Review-g297595-d5897747-Reviews-Aman_Age_Roll_Chicken-Raipur_Raipur_District_Chhattisgarh.html",
minPins: [
['hotel', 20],
['restaurant', 20],
['attraction', 20],
['vacation_rental', 0] ],
units: 'km',
geoMap: false,
tabletFullSite: false,
reuseHoverDivs: false,
noSponsors: true };
ta.store('infobox_js', 'https://static.tacdn.com/js3/infobox-c-v21051733989b.js');
ta.store("ta.maps.apiKey", "");
(function() {
var onload = function() {
if (window.location.hash == "#MAPVIEW") {
ta.run("ta.mapsv2.Factory.handleHashLocation", {}, true);
}
}
if (window.addEventListener) {
if (window.history && window.history.pushState) {
window.addEventListener("popstate", function(e) {
ta.run("ta.mapsv2.Factory.handleHashLocation", {}, false);
}, false);
}
window.addEventListener('load', onload, false);
}
else if (window.attachEvent) {
window.attachEvent('onload', onload);
}
})();
ta.store("mapsv2.show_sidebar", true);
ta.store('mapsv2_restaurant_reservation_js', ["https://static.tacdn.com/js3/ta-mapsv2-restaurant-reservation-c-v2430632369b.js"]);
ta.store('mapsv2.typeahead_css', "https://static.tacdn.com/css2/maps_typeahead-v21940478230b.css");
// Feature gate VR price pins on SRP map. VRC-14803
ta.store('mapsv2.vr_srp_map_price_enabled', true);
ta.store('mapsv2.geoName', 'Raipur');
ta.store('mapsv2.map_addressnotfound', "Address not found"); ta.store('mapsv2.map_addressnotfound3', "We couldn\'t find that location near {0}. Please try another search."); ta.store('mapsv2.directions', "Directions from {0} to {1}"); ta.store('mapsv2.enter_dates', "Enter dates for best prices"); ta.store('mapsv2.best_prices', "Best prices for your stay"); ta.store('mapsv2.list_accom', "List of accommodations"); ta.store('mapsv2.list_hotels', "List of hotels"); ta.store('mapsv2.list_vrs', "List of holiday rentals"); ta.store('mapsv2.more_accom', "More accommodations"); ta.store('mapsv2.more_hotels', "More hotels"); ta.store('mapsv2.more_vrs', "More Holiday Homes"); ta.store('mapsv2.sold_out_on_1', "SOLD OUT on 1 site"); ta.store('mapsv2.sold_out_on_y', "SOLD OUT on 2 sites"); </script>
window.mapDivId='map0Div';
window.map0Div={
纬度:21.25335,
液化天然气:81.649445,
缩放:空,
locId:5897747,
大地水准面:297595,
isAttraction:错误,
是的,
Isloding:错,
伊斯兰堡:错,
标题:“Aman Age Roll&Chicken”,
homeIcon:没错,
url:“/Restaurant\u Review-g297595-d5897747-Reviews-Aman\u Age\u Roll\u Chicken-Raipur\u Raipur\u District\u chattisgarh.html”,
minPins:[
[‘酒店’,20],
[“餐厅”,20],
[‘吸引力’,20],
[“度假租赁”,0]],
单位:公里,
地理地图:错,
tabletFullSite:错误,
重复使用:错误,
无赞助者:真};
ta.store('infobox_js','https://static.tacdn.com/js3/infobox-c-v21051733989b.js');
ta.store(“ta.maps.apiKey”);
(功能(){
var onload=函数(){
if(window.location.hash==“#映射视图”){
run(“ta.mapsv2.Factory.handleHashLocation”,{},true);
}
}
if(window.addEventListener){
if(window.history&&window.history.pushState){
window.addEventListener(“popstate”,函数(e){
run(“ta.mapsv2.Factory.handleHashLocation”,{},false);
},假);
}
window.addEventListener('load',onload,false);
}
else if(窗口附件){
window.attachEvent('onload',onload);
}
})();
ta.store(“mapsv2.show_边栏”,true);
ta.商店('mapsv2_餐厅_预订_js',[”https://static.tacdn.com/js3/ta-mapsv2-restaurant-reservation-c-v2430632369b.js"]);
ta.store('mapsv2.typeahead_css',”https://static.tacdn.com/css2/maps_typeahead-v21940478230b.css");
//功能门VR价格引脚在SRP地图上。VRC-14803
ta.store('mapsv2.vr\u srp\u map\u price\u enabled',true);
ta.商店('mapsv2.geoName','Raipur');
ta.store('mapsv2.map_addressnotfound',“Address notfound”);ta.store('mapsv2.map_addressnotfound3',“我们在{0}附近找不到该位置。请尝试其他搜索。”);存储('mapsv2.directions',“从{0}到{1}的方向”);ta.商店('mapsv2.输入日期',“输入最优惠价格的日期”);ta.商店(“mapsv2.最优惠价格”,“您入住的最优惠价格”);ta.商店(“mapsv2.住宿清单”、“住宿清单”);ta.门店(“mapsv2.酒店列表”、“酒店列表”);ta.商店(“mapsv2.list_vrs”、“假日租赁清单”);ta.商店('mapsv2.更多住宿',“更多住宿”);ta.商店(“mapsv2.更多酒店”,“更多酒店”);ta.商店(“mapsv2.more_vrs”、“更多度假屋”);ta.商店('mapsv2.在1号场地售罄',“在1号场地售罄”);ta.商店('mapsv2.在y上售罄',“在2个站点上售罄”);
您的正则表达式有点混乱。
^l
表示您正在尝试匹配一行中第一个字符的“l”
^\s+(l[an][gt])(:\s+)(\d+\.\d+)
会更好。签出一个regerx分析器工具,例如获取正在发生的事情的明细
这是一个细分
顺序:按顺序匹配以下所有内容
贝金诺弗林
重复
空白字符
一次或多次
捕获组
组号:1
顺序:按顺序匹配以下所有内容
L
阿尼查林[a n]
AnyCharIn[g t]
捕获组
组号:2
顺序:按顺序匹配以下所有内容
:
重复
空白字符
一次或多次
捕获组
组号:3
顺序:按顺序匹配以下所有内容
重复
数字
一次或多次
.
重复
数字
一次或多次
您的正则表达式有点混乱。
^l
表示您正在尝试匹配一行中第一个字符的“l”
^\s+(l[an][gt])(:\s+)(\d+\.\d+)
会更好。签出一个regerx分析器工具,例如获取正在发生的事情的明细
这是一个细分
顺序:按顺序匹配以下所有内容
贝金诺弗林
重复
空白字符
一次或多次
捕获组
组号:1
顺序:按顺序匹配以下所有内容
L
阿尼查林[a n]
AnyCharIn[g t]
捕获组
组号:2
顺序:按顺序匹配以下所有内容
:
重复
空白字符
一次或多次
捕获组
组号:3
顺序:按顺序匹配以下所有内容
重复
数字
一次或多次
.
重复
数字
一次或多次
“(l[an][gt])(:\s+)(\d+\.\d+)“此表达式正在工作”(l[an][gt])(:\s+)(\d+.\d+)“此表达式正在工作。”