从纯HTML中提取数据

时间:2015-09-12 17:39:22

标签: html regex extract

我有一段html,其中包含邮政编码的第一部分及其所在的区域。两行示例:

<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>

如何/我可以使用什么来提取区域和邮政编码的一部分减去位置的名称。

从示例数据中,我想检索('Channel Islands', 'GY'), ('Channel Islands', 'JE'),以便我可以将它们输入数据库。

4 个答案:

答案 0 :(得分:1)

您可以使用正则表达式

/<td.*?>(.*?)<\/td.*?>\s+<td.*?>(.*?)\s-.*?<\/td>/gi

<强> Regex explanation here

答案 1 :(得分:1)

使用GAWK

使用gawk脚本语言可以证明是值得的

创建文件名gawkscript并添加以下内容:

#Test to see if the line matches the region, if it does, use regex to get your region
$0~/<td class="xl66" height="17">Channel Islands<\/td>/{printf gensub(/ *<td.*>(.+)<\/td>/,"\\1,",$0)};

#Test to see if the line matches the cose, if it does, use regex to get your code
$0~/<td class="xl66">(.+)<\/td>/{print gensub(/ *<td.*>(.+) *- \(.+\)<\/td>/,"\\1",$0)}

使用您的shell可以运行脚本

gawk -f gawkscript   my_html_file

由于这是我们正在处理的html代码,我会在python调用中使用强大的html webscraping模块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在提取信息时消除了html代码中不需要的换行符

答案 2 :(得分:0)

我可以在javascript的帮助下完成你的工作,因为我对正则表达式的了解较少。

这里是JS代码:

try_files $uri $uri/ /rtstaging/index.php?$query_string;

这是工作链接

server {
    listen 80;
    listen [::]:80;

    root /home/default/public;
    index index.php index.html index.htm;

    server_name nu-voo.co.uk www.nu-voo.co.uk;
    include hhvm.conf;

    location /rtstaging/ {
            alias  /home/rtsearch/public/;
            try_files $uri $uri/ /rtstaging/index.php?$query_string;
            include hhvm.conf;
    }
}

希望它可以帮助你:)

答案 3 :(得分:0)

在不了解语言(PHP / ASP)和数据库(MySQL / NoSQL)等服务器端细节的情况下,我无法向您展示如何处理后端的代码。

话虽如此,下面的代码向您展示了如何处理和准备数据客户端,以及如何通过JavaScript向您的服务器发出异步(POST)请求。

相关信息:

以下是 - FormData( ) object

的详细说明指南

以下是 - XMLHttpRequest( ) object

的详细说明指南

截图:

enter image description here

// 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;
}