亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python 制作網(wǎng)站篩選工具(附源碼)

 更新時間:2021年01月21日 14:57:47   作者:懷淰メ  
平常工作生活中,boos可能會給我們很多網(wǎng)站取提取信息,這些網(wǎng)站有的無法響應(yīng),有的404,有的501…真的需要所有網(wǎng)站都訪問再提取信息嗎?今天寫一個小工具用于篩選網(wǎng)站中能訪問的網(wǎng)站,在此僅舉一例,即狀態(tài)碼為200。

一.思路

1.整體思路

2.代碼思路

思路很簡單,就是用python發(fā)送請求,提取響應(yīng)體中的狀態(tài)碼加以判斷,最后保存到本地txt文本中,以實現(xiàn)網(wǎng)站信息的篩選。

二.撰寫代碼

import time
import requests
import urllib3
from concurrent.futures import ThreadPoolExecutor

#取源文件中的網(wǎng)址并且去重
def get_url(old_file):
  with open(old_file,'r',encoding='gbk')as f:
    urllist=list(set(f.readlines()))
    return urllist

#主體,發(fā)送請求,通過異常捕獲判斷能否響應(yīng),通過狀態(tài)碼判斷網(wǎng)閘能否正常訪問
def request(url):
  url=url.strip()
  #構(gòu)造請求頭信息
  headers = {
    'Connection': 'keep-alive',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
  }
  try:
    #忽略證書安全警告
    urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
    r=requests.get(url,headers=headers,timeout=10,verify=False)#timeout設(shè)置超時時間,我設(shè)置了10s,使用verif=False忽略sll認證
    if r.status_code==200:
      cost_time=r.elapsed.total_seconds()#計算訪問耗時
      print(url,' ----->【能訪問】訪問耗時:\033[35;46m{:.2f}s\033[0m'.format(cost_time))
      can_access_save_to_txt(url)
    else:
      print(url,' ----->不能訪問,狀態(tài)碼為:\033[31;44m{}\033[0m'.format(r.status_code))
  except:
    print(url, ' ----->不能訪問,原因:\033[31;44m不能響應(yīng)\033[0m')


#將能訪問的網(wǎng)址保存到new_file
def can_access_save_to_txt(result):
  result=result.strip()
  #篩選出不是.apk的url,此判斷可以不加
  if not result.endswith('.apk'):
    with open(aim_file,'a')as f:
      f.write(result+'\n')


def main():
  s_time=time.time()
  #使用線程池,創(chuàng)建四條線程。
  pool=ThreadPoolExecutor(max_workers=4)
  urls=get_url(orign_file)
  for url in urls:
    pool.submit(request,url)
  pool.shutdown()
  e_time=time.time()
  sum_time=int(e_time-s_time)
  if sum_time>60:
    print(f'\033[38;46m 程序正常執(zhí)行結(jié)束退出!共耗時:【{sum_time//60}分鐘】 \033[0m')
  elif sum_time/60>1:
    print(f'\033[38;46m 程序正常執(zhí)行結(jié)束退出!共耗時:【{sum_time//60*60}小時】 \033[0m')


if __name__ == '__main__':
  orign_file=r'E:\test.txt'
  #篩選后能訪問的網(wǎng)址
  aim_file="./data/test_can_access.txt"
  #篩選后不能訪問的網(wǎng)址
  main()

三.運行結(jié)果

四.總結(jié)

本次使用python撰寫了一款篩選網(wǎng)站的小工具,將網(wǎng)站大致分為能訪問和不能訪問兩類,將能夠訪問且狀態(tài)碼為200的網(wǎng)站存儲到了文件中,最終實現(xiàn)了網(wǎng)站的篩選。思路、代碼方面有什么不足歡迎各位大佬指正、批評!

以上就是python 制作網(wǎng)站篩選工具(附源碼)的詳細內(nèi)容,更多關(guān)于python 制作網(wǎng)站篩選工具的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論