python3判断url链接是否为404的方法
程序员文章站
2022-05-28 08:59:14
本文实例为大家分享了python3判断url链接是否为404的具体代码,供大家参考,具体内容如下
import pymysql
import threadi...
本文实例为大家分享了python3判断url链接是否为404的具体代码,供大家参考,具体内容如下
import pymysql import threading import time import urllib import requests # 打开数据库连接 db = pymysql.connect("192.168.0.*", "username", "password", "databasename") # 使用 cursor() 方法创建一个游标对象 cursor cursor = db.cursor() # sql 查询语句 sql = "select sku,url_6 from url_new where flag_6 is null and url_6<>'' " # 执行sql语句 cursor.execute(sql) # 获取所有记录列表 results = cursor.fetchall() num = 0 for row in results: sku = row[0] url = row[1] html = requests.head(url) # 用head方法去请求资源头 re=html.status_code num = num + 1 print(num,re) if re == 200: sql_2 = "update url_new set flag_6 = 0 where sku = '%s'" % sku try: # 执行sql语句 cursor.execute(sql_2) #print(cursor.rowcount) except exception as e: print(e) conn.rollback() if re == 404: sql_3 = "update url_new set flag_6 = 1 where sku = '%s'" % sku try: # 执行sql语句 cursor.execute(sql_3) print(cursor.rowcount) except exception as e: print(e) conn.rollback() db.commit() db.close()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。