梦是一种欲望,想是一种行动。梦想是梦与想的结晶。当心灵趋于平静时,精神便是永恒!把欲望降到最低点,把理性升华到最高点,你会感受到:平安是福,清新是禄,寡欲是寿!
本文实例为大家分享了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()
本文python3判断url链接是否为404的方法到此结束。为世界进文明,为人类造幸福,以青年之我,创建青春之家适青春之国家,青春之民族,青春寄之人类,青春之地球,青春之宇宙,资以乐其无涯生。小编再次感谢大家对我们的支持!