1 import requests 2 import re 3 import pymysql 4 #10页 仔细观察路由 5 db = pymysql.connect("localhost","root","root","testdb" ) 6 cursor = db.cursor() 7 8 for i in range(1,10): 9url = 'http://*******8****'10url=url+'index_'+str(i)+'.html'11response = requests.get(url)12HTML = response.text13pattern= pile(r'(((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3})')14compile_1 = pile(r'\s+(\d{3,6})\s')15compile_2 = pile(r'\s+([\u4e00-\u9fa5]{1,9})\s?(?:省|新疆|内蒙古|市|县|区])')16# compile_4 = pile(r's+(\d{4}/\d{2}/\d{2}\s+\d{2}:\d{2}:\d{2})\s')17res2 = compile_2.findall(HTML) 18res1 = compile_1.findall(HTML)19result = pattern.findall(HTML) 20# res3 = compile_4.findall(HTML)21# print(res3,len(result)) 22for ip_ in result:23 print(ip_[0]) 24 sql = "INSERT INTO test (ip,port,place) VALUES ('%s','%s','%s')" %(ip_[0],res1[0],res2[0])25 print(sql)26 cursor.execute(sql)27 db.close()28 29 # 使用cursor()方法获取操作游标
如果觉得《python爬虫爬取ip记录网站信息并存入数据库》对你有帮助,请点赞、收藏,并留下你的观点哦!