简体   繁体   English

Python:从一些讨厌的HTML中提取数据

[英]Python : extract data from some nasty html

My question is related to usage of HTMLParser but on a bit of nast html code. 我的问题与HTMLParser的使用有关,但仅与一些html代码有关。
I have a file/webpage containing multiple html/css entries and somewhere in bewteen the lines i get this frequently repeated parts of html code i need to parse to extract some certain data. 我有一个包含多个html / css条目的文件/网页,在不到一行的某处,我得到了我经常需要解析的html代码的重复部分,以提取某些数据。

For example: 例如:

1) 1)
Number 66 to be extracted 要提取的数字66
Number 123456 to be extracted ftom this comment 从此评论中提取的编号123456

<h3 class="s KB8NC">66.&hsbc; 
<!--
        <A name="simp123456" href="text.php?p=1&i_simp_z_boc_nb_sec=123456&i_simp_s_vitrazka=1">
-->
ristill advocka, sygint: SURVE/123-021/11-2/XX</h3>


And another frequent entries which show up in pairs: 另一个成对出现的常见条目:

2) 2)
First entry to be ignored because of empty 'data' 由于“数据”为空,第一个条目将被忽略
Number 123456 to extract 提取编号123456

<p class="monozzio"></p>
<p class="monozzio"><a href="text.php?p=1&pup;i_simp_z_boc_nb_sec=123456&pup;i_simp_s_vitrazka=1">monozzio...</a></p> 



Here is my first class so far but it starts to exceed my skills, any help appreciated. 到目前为止,这是我的第一堂课,但是超出了我的技能范围,任何帮助都值得赞赏。

from HTMLParser import HTMLParser
class MyParser(HTMLParser):  
   def __init__(self):
HTMLParser.__init__(self)
self.recording = 0
self.data = []
   def handle_starttag(self, tag, attributes):
     if tag != 'p':
       return
     if self.recording:
       self.recording += 1
       return
     for name, value in attributes:
     if name == 'class' and value == 'monozzio':
        break
     else:
     return
     self.recording = 1

def handle_endtag(self, tag):
  if tag == 'p' and self.recording:
    self.recording -= 1

def handle_data(self, data):
  if self.recording:
    ##############################
    #here parse data to get 123456
    ##############################
  self.data.append(data)

p = MyParser()
f = open('file.html', 'r')
htm = f.read()
p.feed(htm)
print '\n'.join(p.data)
p.close()

This is not directly a solution to your problem, but BeautifulSoup is a library that makes it easier to parse HTML. 这不是直接解决您的问题的方法,但是BeautifulSoup是一个可以更轻松地解析HTML的库。

Then you can do something like: 然后,您可以执行以下操作:

import re
from bs4 import BeautifulSoup

html = BeautifulSoup(your_html_content)
for link in html.find_all('p.monozzio a'):  # use css selectors
    href = link.get('href')
    reg = re.compile('i_simp_z_boc_nb_sec=([0-9]+)')
    nbrs = reg.findall(href)  # regex to extract values

Note that I didn't test the code, it's just a general idea. 请注意,我没有测试代码,这只是一个总体思路。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM