python爬取url的信息_python 模版apipython 爬取指定url的ICP备案信息(结构化抓取)...

该Python脚本用于从Alexa获取指定URL的ICP备案信息,包括公司名称、属性、ICP号等。通过正则表达式解析HTML内容,实现了结构化的数据抓取。
摘要由CSDN通过智能技术生成

#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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值