Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从网站HTMLParser Python获取数据_Python_Html_Parsing_Html Parsing - Fatal编程技术网

从网站HTMLParser Python获取数据

从网站HTMLParser Python获取数据,python,html,parsing,html-parsing,Python,Html,Parsing,Html Parsing,我想考虑网站的三个组成部分,首先是Ipadress、port和protoco,我使用python中的HTMLParser来解决这个问题,但是下面标记中的代码没有属性和值 代理列表 192.168.1.10 HTTP1 越南 8080 A. %d1 10.25.100.10 HTTPS2 坎布希亚 3214 B %d2 203.25.10.110 HTTP3 泰兰 123 C %d3 220.155.10.13 HTTP4 意大利 1000 D %d4 220.155.10.113 H

我想考虑网站的三个组成部分,首先是Ipadress、port和protoco,我使用python中的HTMLParser来解决这个问题,但是下面标记中的代码没有属性和值

代理列表 192.168.1.10 HTTP1 越南 8080 A. %d1 10.25.100.10 HTTPS2 坎布希亚 3214 B %d2 203.25.10.110 HTTP3 泰兰 123 C %d3 220.155.10.13 HTTP4 意大利 1000 D %d4 220.155.10.113 HTTP5 意大利 505 D %d4 220.155.10.115 HTTPS6 意大利 321 D %d4 尝试使用lxml:


你必须自己添加标签。我同意Andres的观点,lxml更适合于此,但使用HTMLParser可以创建一个TableParser类,该类输出一个嵌套数组,该数组包含每个表行的字典

import HTMLParser
html = """<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>proxy-list</title>
</head>
    <body>
        <table>
            <tr>
                <td>192.168.1.10</td>     
                <td>HTTP1</td>
                <td>Vietnam</td>
                <td>8080</td>
                <td>a</td>
                <td>%d1</td>
            </tr>
            <tr>
                <td>10.25.100.10</td>
                <td>HTTPS2</td>
                <td>Campuchia</td>
                <td>3214</td>
                <td>b</td>
                <td>%d2</td>
            </tr>       
        </table>
    </body>
</html>"""

class TableParser(HTMLParser.HTMLParser):
   def __init__(self,tags):
       self.tags=tags
       HTMLParser.HTMLParser.__init__(self)
       self.in_td = False  

   def handle_starttag(self, tag, attrs):
        if tag == 'td':           
           self.in_td = True           
        if tag == 'tr':
            self.count = 0
            self.row = {}

   def handle_data(self, data):
       if self.in_td:           
           self.row[self.tags[self.count]] = data
           self.count = self.count +1          

   def handle_endtag(self, tag):
      self.in_td = False
      if tag == 'tr':
            self.table.append(self.row)

   def feed_tags(self,html,tags):  
       self.table=[]       
       self.feed(html)
       return self.table

tags=  ['ip','protocol','country','port','field1','field2'] 
p = TableParser(tags)    
table = p.feed_tags(html,tags)
for row in table:
    print row['ip']+','+row['protocol']+','+row['port']

你试过什么吗?所有的积木都是一样的结构吗?我看到第三个街区没有港口。不要粘贴代码作为答案,只需编辑您的问题。的结构完全相同,但我想用HTMLPasser编写,没有Andrés Pérez Albela H?对不起Andrés Pérez Abela H,我非常感谢您的帮助Jaco:为什么?脚本是刚刚得到一个数组的最后一个是?帮帮我?我不知道你在问什么。我已经修改了我的脚本,以返回一个嵌套数组,该数组只包含所需的详细信息。哦,不,我希望得到两个值:192.168.1.10,http18080和10.25.100.10,HTTP2,3214。帮帮我:DYour脚本打印出相同的列表:[{'protocol':'HTTPS2',ip':'10.25.100.10','port':'3214'},{'protocol':'HTTPS2',ip':'10.25.100.10','port':'3214'}]帮帮我,Jaco,我尝试过编辑,但我无法纠正:
IP: 192.168.1.10
Protocol: HTTP1
Port: 8080

IP: 10.25.100.10
Protocol: HTTPS2
Port: 3214

IP: 203.25.10.110
Protocol: HTTP3
Port: 3215

IP: 220.155.10.13
Protocol: HTTP4
Port: 1000

IP: 220.155.10.13
Protocol: HTTP5
Port: 1000

IP: 220.155.10.13
Protocol: HTTP6
Port: 1000
import HTMLParser
html = """<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>proxy-list</title>
</head>
    <body>
        <table>
            <tr>
                <td>192.168.1.10</td>     
                <td>HTTP1</td>
                <td>Vietnam</td>
                <td>8080</td>
                <td>a</td>
                <td>%d1</td>
            </tr>
            <tr>
                <td>10.25.100.10</td>
                <td>HTTPS2</td>
                <td>Campuchia</td>
                <td>3214</td>
                <td>b</td>
                <td>%d2</td>
            </tr>       
        </table>
    </body>
</html>"""

class TableParser(HTMLParser.HTMLParser):
   def __init__(self,tags):
       self.tags=tags
       HTMLParser.HTMLParser.__init__(self)
       self.in_td = False  

   def handle_starttag(self, tag, attrs):
        if tag == 'td':           
           self.in_td = True           
        if tag == 'tr':
            self.count = 0
            self.row = {}

   def handle_data(self, data):
       if self.in_td:           
           self.row[self.tags[self.count]] = data
           self.count = self.count +1          

   def handle_endtag(self, tag):
      self.in_td = False
      if tag == 'tr':
            self.table.append(self.row)

   def feed_tags(self,html,tags):  
       self.table=[]       
       self.feed(html)
       return self.table

tags=  ['ip','protocol','country','port','field1','field2'] 
p = TableParser(tags)    
table = p.feed_tags(html,tags)
for row in table:
    print row['ip']+','+row['protocol']+','+row['port']