zoukankan      html  css  js  c++  java
  • 那墙可有五十米高啊!

    刚写完用了两天数据源就被封了2333333

    收到通知暂停更新,稍后会删除该文,期待官方解禁。

    ===========================================

    https://github.com/mlxy/GoogleHostsUpdate

    简单的读页面源码然后正则匹配。

    我只是懒得自己更新hosts。

    为了chrome的同步我呕心沥血。

    请和之前的汤站爬虫一起加进计划任务里。

    面向过程充满了爱。

     1 #encoding:utf-8
     2 import urllib
     3 import re
     4 import os
     5 
     6 url = 'http://www.360kb.com/kb/2_122.html'
     7 regexHosts = r'#google hosts 2015 by 360kb.com.*#google hosts 2015 end'
     8 regexTimeUpdated = r'<strong>(dddd.dd?.dd?)&nbsp;</strong>'
     9 
    10 hostsPath = 'C:\Windows\System32\drivers\etc\hosts'
    11     
    12 def retrievePage(url):
    13     ''' 读取页面源代码。 '''
    14     response = urllib.urlopen(url)
    15     page = response.read()
    16     return page
    17 
    18 def matchTimeUpdated(page):
    19     ''' 从页面源码中匹配出hosts更新时间。 '''
    20     timeUpdated = re.search(regexTimeUpdated, page)
    21     return timeUpdated.group(1)
    22 
    23 def matchHostList(page):
    24     ''' 从页面源码中匹配出host列表。 '''
    25     result = re.search(regexHosts, page, re.S)
    26     hosts = result.group()
    27     return hosts
    28     
    29 def translateSpaceEntity(srcString):
    30     ''' 把结果中的"&nbsp;"转换成空格。 '''
    31     return srcString.replace('&nbsp;', ' ')
    32     
    33 def removeHtmlLabels(srcString):
    34     ''' 去除结果中的HTML标签。 '''
    35     return re.sub(r'<[^>]+>', '', srcString)
    36 
    37 def addExtraInfo(srcString, extraInfo):
    38     ''' 在第一行添加额外信息。'''
    39     return extraInfo + '
    ' + srcString
    40 
    41 def write2File(hosts, filePath):
    42     ''' 写出到文件。 '''
    43     f = open(filePath, 'w')
    44     f.write(hosts)
    45     f.close()
    46     
    47 def run():
    48     ''' 主运行函数。 '''
    49     page = retrievePage(url)
    50     
    51     roughHosts = matchHostList(page)
    52     preciseHosts = removeHtmlLabels(translateSpaceEntity(roughHosts))
    53     
    54     extraInfo = '''#Hosts updated at %s
    55 #Script written by mlxy@https://github.com/mlxy, feel free to modify and distribute it.
    56 ''' %matchTimeUpdated(page)
    57     hostsWithExtra = addExtraInfo(preciseHosts, extraInfo)
    58     
    59     write2File(hostsWithExtra, hostsPath)
    60     
    61 if __name__ == '__main__':
    62     run()
    而正则表达式则难写的一逼
  • 相关阅读:
    Jmeter接口测试时传递json格式的数据
    selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element 定位frame中的元素
    selenium报错“selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.”的解决方案
    python+selenium如何定位页面的元素,有几种定位元素的方法?
    c#中的表达式
    占位符的使用
    数据类型(变量的声明与赋值)
    Hello World 老调重谈
    易语言转C#小试牛刀
    开博了
  • 原文地址:https://www.cnblogs.com/chihane/p/4239419.html
Copyright © 2011-2022 走看看