≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > Python

python 结构化抓取指定url的ICP备案信息示例

人气:691 时间:2018-10-24

这篇文章主要为大家详细介绍了python 结构化抓取指定url的ICP备案信息示例,具有一定的参考价值,可以用来参考一下。

对python这个高级语言感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!

# @param python 爬取指定url的ICP备案信息(结构化抓取)
# @author 四海网|www.q1010.com 

#coding=gbk  
import os 
import sys 
import re 
import time 
import urllib2 
  
def perror_and_exit(message, status = -1): 
    sys.stderr.write(message + '\n') 
    sys.exit(status) 
  
def get_text_from_html_tag(html): 
    pattern_text =  re.compile(r">.*?    return pattern_text.findall(html)[0][1:-2].strip() 
  
def parse_alexa(url): 
    url_alexa = "http://icp.alexa.cn/index.php?q=%s" % url 
    print url_alexa 
    #handle exception  
    times = 0
    while times < 5000: #等待有一定次数限制  
        try: 
            alexa = urllib2.urlopen(url_alexa).read() 
  
            pattern_table = re.compile(r".*?", re.DOTALL | re.MULTILINE) 
            match_table = pattern_table.search(alexa) 
            if not match_table: 
                raise BaseException("No table in HTML") 
            break
        except: 
            print "try %s times:sleep %s seconds" % (times, 2**times) 
            times += 1 
            time.sleep(2**times) 
            continue
  
    table = match_table.group() 
    pattern_tr = re.compile(r".*?", re.DOTALL | re.MULTILINE) 
    match_tr = pattern_tr.findall(table) 
    if len(match_tr) != 2: 
        perror_and_exit("table format is incorrect") 
      
    icp_tr = match_tr[1] 
    pattern_td = re.compile(r".*?", re.DOTALL | re.MULTILINE) 
    match_td = pattern_td.findall(icp_tr) 
      
    #print match_td  
    company_name = get_text_from_html_tag(match_td[1]) 
    company_properties = get_text_from_html_tag(match_td[2]) 
    company_icp = get_text_from_html_tag(match_td[3]) 
    company_icp = company_icp[company_icp.find(">") + 1:] 
    company_website_name = get_text_from_html_tag(match_td[4]) 
    company_website_home_page = get_text_from_html_tag(match_td[5]) 
    company_website_home_page = company_website_home_page[company_website_home_page.rfind(">") + 1:] 
    company_detail_url = get_text_from_html_tag(match_td[7]) 
    pattern_href = re.compile(r"href=\".*?\"", re.DOTALL | re.MULTILINE) 
    match_href = pattern_href.findall(company_detail_url) 
    if len(match_href) == 0: 
        company_detail_url = "" 
    else: 
        company_detail_url = match_href[0][len("href=\""):-1] 
    return [url, company_name, company_properties, company_icp, company_website_name, company_website_home_page, company_detail_url] 
    pass
  
if __name__ == "__main__": 
    fw = file("out.txt", "w") 
    for url in sys.stdin: 
        fw.write("\t".join(parse_alexa(url)) + "\n") 
 
#coding=gbk
import os
import sys
import re
import time
import urllib2
 
def perror_and_exit(message, status = -1):
    sys.stderr.write(message + '\n')
    sys.exit(status)
 
def get_text_from_html_tag(html):
    pattern_text =  re.compile(r">.*?    return pattern_text.findall(html)[0][1:-2].strip()
 
def parse_alexa(url):
    url_alexa = "http://icp.alexa.cn/index.php?q=%s" % url
    print url_alexa
    #handle exception
    times = 0
    while times < 5000: #等待有一定次数限制
        try:
            alexa = urllib2.urlopen(url_alexa).read()
 
            pattern_table = re.compile(r".*?", re.DOTALL | re.MULTILINE)
            match_table = pattern_table.search(alexa)
            if not match_table:
                raise BaseException("No table in HTML")
            break
        except:
            print "try %s times:sleep %s seconds" % (times, 2**times)
            times += 1
            time.sleep(2**times)
            continue
 
    table = match_table.group()
    pattern_tr = re.compile(r".*?", re.DOTALL | re.MULTILINE)
    match_tr = pattern_tr.findall(table)
    if len(match_tr) != 2:
        perror_and_exit("table format is incorrect")
    
    icp_tr = match_tr[1]
    pattern_td = re.compile(r".*?", re.DOTALL | re.MULTILINE)
    match_td = pattern_td.findall(icp_tr)
    
    #print match_td
    company_name = get_text_from_html_tag(match_td[1])
    company_properties = get_text_from_html_tag(match_td[2])
    company_icp = get_text_from_html_tag(match_td[3])
    company_icp = company_icp[company_icp.find(">") + 1:]
    company_website_name = get_text_from_html_tag(match_td[4])
    company_website_home_page = get_text_from_html_tag(match_td[5])
    company_website_home_page = company_website_home_page[company_website_home_page.rfind(">") + 1:]
    company_detail_url = get_text_from_html_tag(match_td[7])
    pattern_href = re.compile(r"href=\".*?\"", re.DOTALL | re.MULTILINE)
    match_href = pattern_href.findall(company_detail_url)
    if len(match_href) == 0:
        company_detail_url = ""
    else:
        company_detail_url = match_href[0][len("href=\""):-1]
    return [url, company_name, company_properties, company_icp, company_website_name, company_website_home_page, company_detail_url]
    pass
 
if __name__ == "__main__":
    fw = file("out.txt", "w")
    for url in sys.stdin:
        fw.write("\t".join(parse_alexa(url)) + "\n")[python] view plaincopyprint? time.sleep(2) 
    pass
 
 time.sleep(2)
    pass

# End www_512pic_com

 

每次抓取都会sleep 2s,防止ip被封,实际上即使sleep了IP过一段时间还是会被封

由于是结构化抓取,当网站格式变化此程序将无法使用

本文来自:http://www.q1010.com/181/2337-0.html

注:关于python 结构化抓取指定url的ICP备案信息示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:备案

您可能感兴趣的文章

上一篇:python对多线程的支持解析
下一篇:Python读取和写入mp3文件的id3v1信息示例
热门文章
  • Python 处理Cookie的菜鸟教程(一)Cookie库
  • python之pandas取dataframe特定行列的简单示例
  • Python解决json.dumps错误::‘utf8’ codec can‘t decode byte
  • Python通过pythony连接Hive执行Hql的脚本
  • Python 三种方法删除列表中重复元素的简单示例
  • python爬虫代码示例
  • Python 中英文标点转换示例
  • Python 不得不知的开源项目解析
  • Python urlencode编码和url拼接实现方法
  • python按中文拆分中英文混合字符串的简单示例
  • 最新文章
    • Python利用numpy三层神经网络的简单示例
    • pygame可视化幸运大转盘的简单示例
    • Python爬虫之爬取二手房信息的简单示例
    • Python之time库的简单示例
    • OpenCV灰度、高斯模糊、边缘检测的简单示例
    • Python安装Bs4及使用的简单示例
    • django自定义manage.py管理命令的简单示例
    • Python之matplotlib 向任意位置添加一个子图(axes)的简单示例
    • Python图像标签标注软件labelme分析的简单示例
    • python调用摄像头并拍照发邮箱的简单示例

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。