• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

3种Python检测URL状态

python 搞java代码 3年前 (2022-05-21) 19次浏览 已收录 0个评论

需求:Python检测URL状态,并追加保存200的URL

代码一:

<code class="language-python">#! /usr/bin/env <a href="https://www.gaodaima.com/tag/python" title="查看更多关于python的文章" target="_blank">python</a>
#coding=utf-8
import sys
import requests
def getHttpStatusCode(<a href="https://www.gaodaima.com/tag/url" title="查看更多关于url的文章" target="_blank">url</a>):
    try:
        request = requests.get(url)
        httpStatusCode = request.status_code
        return httpStatusCode
    except requests.exceptions.HTTPError as e:
        return e
 
if __name__ == "__main__":
    with open("1.txt", "r") as f:
        for line in f:
            try:
                status = getHttpStatusCode(line.strip("
"))#换行符
                if status == 200:
                    with open("200.txt","a") as f:
                        f.write(line + "
")
                        print line
                else:
                    print "no 200 code"
            except Exception as e:
                print e
</code>

www#gaodaima.com来源gao@dai!ma.com搞$代^码网搞代码

代码二:

<code class="language-python">#! /usr/bin/env python
# -*--coding:utf-8*-
#Python学习交流群:778463939

import requests

def request_status(line):
    conn = requests.get(line)
    if conn.status_code == 200:
        with open("url_200.txt", "a") as f:
            f.write(line + "
")
        return line13     else:
        return None


if __name__ == "__main__":
    with open("/1.txt", "rb") as f:
        for line in f:
            try:
                purge_url = request_status(line.strip("
"))
            except Exception as e:
                pass
</code>

代码三:

<code class="language-python">#! /usr/bin/env python
#coding:utf-8
import os,urllib,linecache
import sys
result = list()

for x in linecache.updatecache(r"1.txt"):
    try:
       a = urllib.urlopen(x.replace("/n","")).getcode()
       #print x,a
    except Exception,e:
        print e
    if a == 200:
        #result.append(x)                             #保存
        #result.sort()                                       #排序结果
        #open("2.txt", "w").write("%s" % "
".join(result)) #保存入结果文件
        with open ("200urllib.txt","a") as f: ## r只读,w可写,a追加
            f.write(x + "
")
    else:
        print "error"
</code>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:3种Python检测URL状态
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址