<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

        python爬取指定url的ICP備案信息

        來源:懂視網(wǎng) 責(zé)編:小采 時間:2020-11-27 14:28:26
        文檔

        python爬取指定url的ICP備案信息

        python爬取指定url的ICP備案信息:#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
        推薦度:
        導(dǎo)讀python爬取指定url的ICP備案信息:#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

        #coding=gbk 
        import os
        import sys
        import re
        import time
        import urllib2
         
        def perror_and_exit(message, status = -1):
         sys.stderr.write(message + '
        ')
         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: #等待有一定次數(shù)限制 
         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("	".join(parse_alexa(url)) + "
        ")
         
        #coding=gbk
        import os
        import sys
        import re
        import time
        import urllib2
         
        def perror_and_exit(message, status = -1):
         sys.stderr.write(message + '
        ')
         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: #等待有一定次數(shù)限制
         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("	".join(parse_alexa(url)) + "
        ")[python] view plaincopyprint? time.sleep(2)
         pass
         
         time.sleep(2)
         pass

        每次抓取都會sleep 2s,防止ip被封,實際上即使sleep了IP過一段時間還是會被封

        由于是結(jié)構(gòu)化抓取,當(dāng)網(wǎng)站格式變化此程序?qū)o法使用

        聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        python爬取指定url的ICP備案信息

        python爬取指定url的ICP備案信息:#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
        推薦度:
        標(biāo)簽: 備案 指定的 python
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 亚洲毛片av日韩av无码| 四虎成年永久免费网站| 在人线av无码免费高潮喷水| 久久精品视频亚洲| 国产成人自产拍免费视频| 午夜免费福利在线观看| 亚洲一区免费视频| 一级特黄aa毛片免费观看| 亚洲AV永久无码区成人网站| 国产三级在线免费观看| 亚洲综合精品网站在线观看| 色屁屁www影院免费观看视频| 午夜私人影院免费体验区| 久久亚洲国产成人影院| 无人在线观看完整免费版视频| 亚洲高清不卡视频| 18禁止看的免费污网站 | 在线观看免费污视频| 亚洲色图.com| 精品免费久久久久久久| 4444亚洲国产成人精品| 97国产免费全部免费观看| 亚洲中文字幕久在线| 最近高清国语中文在线观看免费| 亚洲AV色吊丝无码| 午夜dj免费在线观看| 国产成人高清亚洲一区91| 亚洲熟伦熟女新五十路熟妇| 一道本不卡免费视频| 国产亚洲婷婷香蕉久久精品| 91福利免费网站在线观看| 亚洲成色www久久网站夜月| 99精品一区二区免费视频| 亚洲春黄在线观看| 免费看香港一级毛片| 国产亚洲综合久久| 亚洲国产精品无码av| 亚洲免费福利在线视频| 校园亚洲春色另类小说合集| 在线亚洲人成电影网站色www| 国产在线精品免费aaa片|