首頁  >  問答  >  主體

python爬虫 - Python 爬虫 提取网页信息


爬取网址是:http://www.xici.net.co/nn/1
以上是HTML网页内容,
需获取IP地址,端口号,地方,是否高匿,两个时间

一下是我写的Python,但只能实现部分,请各位大神指点下
谢谢。。。。

import re
import urllib

a = raw_input('input url:')

s = urllib.urlopen(a)
s1 = s.read()


def getinfo(aaa):
    #reg = re.compile(r'(?<![\.\d])(?:\d{1,3}\.){3}\d{1,3}(?![\.\d])')
    #reg = re.compile(r'<td>(\d+)\.(\d+)\.(\d+)\.(\d+)</td>\s*<td>(\d+)</td>\s*<td>([/u4e00-/u9fa5]+)</td>')
    reg = re.compile(r'<td>(\w+)</td>\s*<td>([\u4e00-\u9fa5]+)</td>')
    l = re.findall(reg, aaa)
    print l
getinfo(s1)

结果是类似下面的,不一定是表格

|ip|端口号|位置|是否高匿|类型|速度|连接时间|验证时间|
|-|-|-|-|-|-|-|-|-|
|122.89.9.70|80|台湾|高匿|HTTP|1.27秒|0.325秒|15-08-28 16:30|
|123.69.48.45|8080|江苏南京|高匿|HTTPS|1.07秒|0.5秒|15-08-28 17:30|

黄舟黄舟2712 天前399

全部回覆(8)我來回復

  • 高洛峰

    高洛峰2017-04-17 15:50:25

    以下程式碼可以解決了,謝謝各位的解答。 。 。

    import requests
    from bs4 import BeautifulSoup
    
    
    def getInfo(url):
        proxy_info = []
        page_code = requests.get(url).text
        soup = BeautifulSoup(page_code)
        table_soup = soup.find('table')
        proxy_list = table_soup.findAll('tr')[1:]
        for tr in proxy_list:
            td_list = tr.findAll('td')
            ip = td_list[2].string
            port = td_list[3].string
            location = td_list[4].string or td_list[4].find('a').string
            anonymity = td_list[5].string
            proxy_type = td_list[6].string
            speed = td_list[7].find('p', {'class': 'bar'})['title']
            connect_time = td_list[8].find('p', {'class': 'bar'})['title']
            validate_time = td_list[9].string
    
            # strip
            l = [ip, port, location, anonymity, proxy_type, speed, connect_time, validate_time]
            for i in range( len(l) ):
                if l[i]:
                    l[i] = l[i].strip()
            proxy_info.append(l)
    
        return proxy_info
    
    if __name__ == '__main__':
        url = 'http://www.xici.net.co/nn/1'
        proxy_info = getInfo(url)
        for row in proxy_info:
            for s in row:
                print s,
            print

    回覆
    0
  • 大家讲道理

    大家讲道理2017-04-17 15:50:25

    用xpath去找吧。 。 lxml解析

    回覆
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 15:50:25

    感覺正規表示式可能有點問題。

    首先看文檔結構:

    每一個...標籤裡包含了一列完整的內容,而...標籤裡是一個單項內容。 <tr>...</tr>标签里包含了一列完整的内容,而<td>...</td>标签里是一个单项内容。

    建议用正则表达是从<tr>标签开始对每一个<td>标签进行解析。

    大概这样:r'(<tr class.*?>.*?<td.*?<td.*?<td>(.*?)</td>.......</tr>)'

    这里面(.*?)

    建議用正規表示是從標籤開始對每一個標籤進行解析。

    大概這樣:r'(.*?(.*?).......< /tr>)'

    這裡面(.*?)就是解析出來的ip位址了,後面類似。 🎜 🎜寫起來有點麻煩,但應該不會錯。 🎜 🎜其實用BeautifulSoup會簡單很多。 🎜

    回覆
    0
  • 大家讲道理

    大家讲道理2017-04-17 15:50:25

    用re來操作html,也是醉了,xpath吧。

    回覆
    0
  • 大家讲道理

    大家讲道理2017-04-17 15:50:25

    推薦用BeautifulSoup

    回覆
    0
  • 大家讲道理

    大家讲道理2017-04-17 15:50:25

    BeautifulSoup 是一個很好的選擇,自己寫正規表示式程式碼也顯得不夠優雅。

    回覆
    0
  • PHPz

    PHPz2017-04-17 15:50:25

    ……scrapy呀

    回覆
    0
  • 迷茫

    迷茫2017-04-17 15:50:25

    刮擦...

    回覆
    0
  • 取消回覆