从纯文本中提取数据 HTML
Extract Data From Plain HTML
我有一个 html 的片段,其中包含 post 代码的第一部分及其所在的区域。两行的示例:
<tr height="17">
<td class="xl66" height="17">Channel Islands</td>
<td class="xl66">GY - (Guernsey)</td>
</tr>
<tr height="17">
<td class="xl66" height="17">Channel Islands</td>
<td class="xl66">JE - (Jersey)</td>
</tr>
How/what 我可以用来提取区域和 post 代码减去位置名称的部分。
我想从示例数据中检索 ('Channel Islands', 'GY'), ('Channel Islands', 'JE')
,以便将它们输入数据库。
您可以使用正则表达式
/<td.*?>(.*?)<\/td.*?>\s+<td.*?>(.*?)\s-.*?<\/td>/gi
我可以在 javascript 的帮助下完成您的工作,因为我对正则表达式的了解较少。
这里是 JS 代码:
var array = [];
var table = document.getElementById('myTable');
var rowLength = table.rows.length;
for (i = 0; i < rowLength; i++) {
var collect = [];
var col = table.rows.item(i).cells;
var colLength = col.length;
for (var j = 0; j < colLength; j++) {
var value = col.item(j).innerHTML;
collect.push(value);
}
array.push(collect);
}
这是工作link
http://plnkr.co/edit/8e67Il0u5dmr6NZX3FWJ?p=preview
希望对你有帮助:)
在不了解语言 (PHP/ASP) 和数据库 (MySQL/NoSQL) 等服务器端细节的情况下,我无法向您展示如何处理后端代码。
也就是说,下面的代码向您展示了如何在客户端处理和准备数据,以及如何通过 JavaScript 向您的服务器发出异步 (POST) 请求。
相关信息:
这里有一个有据可查的指南——FormData( ) object
这里有一个有据可查的指南——XMLHttpRequest( ) object
截图:
//HTML (index.html)
<!DOCTYPE html>
<html>
<head>
<script src="index.js"></script>
<link rel="stylesheet" href="index.css">
</head>
<body>
<button onclick="submitTable();">Click Me</button>
<table>
<thead>
<tr>
<th>Region</th>
<th>Postcode</th>
</tr>
</thead>
<tbody>
<tr class="data-row">
<td>Channel Islands</td>
<td>GY - (Guernsey)</td>
</tr>
<tr class="data-row">
<td>Channel Islands</td>
<td>JE - (Jersey)</td>
</tr>
</tbody>
</table>
</body>
</html>
//JS (index.js)
function submitTable(){
//OBJECT INITIALIZATION
if (window.XMLHttpRequest){
var request = new XMLHttpRequest(); // IE7+, Firefox, Chrome, Opera, Safari
}
else{
var request = new ActiveXObject("Microsoft.XMLHTTP"); // IE 6 and older
}
var formData = new FormData();
//LOOP THROUGH DATA and APPEND to FormData(object)
var dataRows = document.getElementsByClassName("data-row");
for(var i=0; i < dataRows.length; i++){
var data = dataRows[i].children;
for(var x=0; x < data.length; x++){
if(x === 1){
var postcode = data[x].innerHTML.substr(0, 2);
formData.append("postcode", data[x].innerHTML);
}
else{
formData.append("region", data[x].innerHTML);
}
}
}
//OPEN
request.open("POST", "/path/to/post/the/data");
//SEND
request.send(formData);
//RESPONSE
request.onreadystatechange = function(){
if(request.readyState == 4){
console.log(request.response);
}
}
}
//CSS (index.css)
table, th, td {
border: 1px solid black;
}
使用 GAWK
使用 gawk 脚本语言是值得的
创建一个文件名 gawkscript
并添加以下内容:
#Test to see if the line matches the region, if it does, use regex to get your region
[=10=]~/<td class="xl66" height="17">Channel Islands<\/td>/{printf gensub(/ *<td.*>(.+)<\/td>/,"\1,",[=10=])};
#Test to see if the line matches the cose, if it does, use regex to get your code
[=10=]~/<td class="xl66">(.+)<\/td>/{print gensub(/ *<td.*>(.+) *- \(.+\)<\/td>/,"\1",[=10=])}
使用你的shell你可以运行脚本
gawk -f gawkscript my_html_file
由于这是我们正在处理的 html 代码,我会在 python 调用 BeautifulSoup
中使用功能强大的 html 网络抓取模块
使用 PYTHON
假设你知道一些 python
import re #import regex
from bs4 import BeautifulSoup #import beautifulsoup
soup=BeautifulSoup(
#add your html string
"""<tr height="17">
<td class="xl66" height="17">Channel Islands</td>
<td class="xl66">GY - (Guernsey)</td>
</tr>
<tr height="17">
<td class="xl66" height="17">Channel Islands</td>
<td class="xl66">JE - (Jersey)</td>
</tr>""")
#find all tr tags in your html string
find_all_tr=soup.find_all("tr")
for i in find_all_tr: #loop through all tr tags
all_td=BeautifulSoup(str(i)).find_all("td") #find all td tags
for j in all_td:
#check to see if your td tages matches 'td class="xl66" height="17"' and print
if re.search('<td class="xl66" height="17"',str(j)):
print(j.text.strip(),end=" ")
#check to see if your td tag matches ONLY <td class="xl66" and use regex to obtain your country code
elif re.search('<td class="xl66">',str(j)):
print(","+re.sub(".*-","",j.text.strip()))
注意:我在这里使用strip()函数是因为html在提取信息[=15=时消除html代码中不需要的换行符]
我有一个 html 的片段,其中包含 post 代码的第一部分及其所在的区域。两行的示例:
<tr height="17">
<td class="xl66" height="17">Channel Islands</td>
<td class="xl66">GY - (Guernsey)</td>
</tr>
<tr height="17">
<td class="xl66" height="17">Channel Islands</td>
<td class="xl66">JE - (Jersey)</td>
</tr>
How/what 我可以用来提取区域和 post 代码减去位置名称的部分。
我想从示例数据中检索 ('Channel Islands', 'GY'), ('Channel Islands', 'JE')
,以便将它们输入数据库。
您可以使用正则表达式
/<td.*?>(.*?)<\/td.*?>\s+<td.*?>(.*?)\s-.*?<\/td>/gi
我可以在 javascript 的帮助下完成您的工作,因为我对正则表达式的了解较少。
这里是 JS 代码:
var array = [];
var table = document.getElementById('myTable');
var rowLength = table.rows.length;
for (i = 0; i < rowLength; i++) {
var collect = [];
var col = table.rows.item(i).cells;
var colLength = col.length;
for (var j = 0; j < colLength; j++) {
var value = col.item(j).innerHTML;
collect.push(value);
}
array.push(collect);
}
这是工作link
http://plnkr.co/edit/8e67Il0u5dmr6NZX3FWJ?p=preview
希望对你有帮助:)
在不了解语言 (PHP/ASP) 和数据库 (MySQL/NoSQL) 等服务器端细节的情况下,我无法向您展示如何处理后端代码。
也就是说,下面的代码向您展示了如何在客户端处理和准备数据,以及如何通过 JavaScript 向您的服务器发出异步 (POST) 请求。
相关信息:
这里有一个有据可查的指南——FormData( ) object
这里有一个有据可查的指南——XMLHttpRequest( ) object
截图:
//HTML (index.html)
<!DOCTYPE html>
<html>
<head>
<script src="index.js"></script>
<link rel="stylesheet" href="index.css">
</head>
<body>
<button onclick="submitTable();">Click Me</button>
<table>
<thead>
<tr>
<th>Region</th>
<th>Postcode</th>
</tr>
</thead>
<tbody>
<tr class="data-row">
<td>Channel Islands</td>
<td>GY - (Guernsey)</td>
</tr>
<tr class="data-row">
<td>Channel Islands</td>
<td>JE - (Jersey)</td>
</tr>
</tbody>
</table>
</body>
</html>
//JS (index.js)
function submitTable(){
//OBJECT INITIALIZATION
if (window.XMLHttpRequest){
var request = new XMLHttpRequest(); // IE7+, Firefox, Chrome, Opera, Safari
}
else{
var request = new ActiveXObject("Microsoft.XMLHTTP"); // IE 6 and older
}
var formData = new FormData();
//LOOP THROUGH DATA and APPEND to FormData(object)
var dataRows = document.getElementsByClassName("data-row");
for(var i=0; i < dataRows.length; i++){
var data = dataRows[i].children;
for(var x=0; x < data.length; x++){
if(x === 1){
var postcode = data[x].innerHTML.substr(0, 2);
formData.append("postcode", data[x].innerHTML);
}
else{
formData.append("region", data[x].innerHTML);
}
}
}
//OPEN
request.open("POST", "/path/to/post/the/data");
//SEND
request.send(formData);
//RESPONSE
request.onreadystatechange = function(){
if(request.readyState == 4){
console.log(request.response);
}
}
}
//CSS (index.css)
table, th, td {
border: 1px solid black;
}
使用 GAWK
使用 gawk 脚本语言是值得的
创建一个文件名 gawkscript
并添加以下内容:
#Test to see if the line matches the region, if it does, use regex to get your region
[=10=]~/<td class="xl66" height="17">Channel Islands<\/td>/{printf gensub(/ *<td.*>(.+)<\/td>/,"\1,",[=10=])};
#Test to see if the line matches the cose, if it does, use regex to get your code
[=10=]~/<td class="xl66">(.+)<\/td>/{print gensub(/ *<td.*>(.+) *- \(.+\)<\/td>/,"\1",[=10=])}
使用你的shell你可以运行脚本
gawk -f gawkscript my_html_file
由于这是我们正在处理的 html 代码,我会在 python 调用 BeautifulSoup
使用 PYTHON 假设你知道一些 python
import re #import regex
from bs4 import BeautifulSoup #import beautifulsoup
soup=BeautifulSoup(
#add your html string
"""<tr height="17">
<td class="xl66" height="17">Channel Islands</td>
<td class="xl66">GY - (Guernsey)</td>
</tr>
<tr height="17">
<td class="xl66" height="17">Channel Islands</td>
<td class="xl66">JE - (Jersey)</td>
</tr>""")
#find all tr tags in your html string
find_all_tr=soup.find_all("tr")
for i in find_all_tr: #loop through all tr tags
all_td=BeautifulSoup(str(i)).find_all("td") #find all td tags
for j in all_td:
#check to see if your td tages matches 'td class="xl66" height="17"' and print
if re.search('<td class="xl66" height="17"',str(j)):
print(j.text.strip(),end=" ")
#check to see if your td tag matches ONLY <td class="xl66" and use regex to obtain your country code
elif re.search('<td class="xl66">',str(j)):
print(","+re.sub(".*-","",j.text.strip()))
注意:我在这里使用strip()函数是因为html在提取信息[=15=时消除html代码中不需要的换行符]