python获取ip存放mysql数据库与csv文件,后续使用ip代理
import requests,csv, pymysql
from lxml import etree
def ips():
try:
db = pymysql.connect(host="localhost", user="root", password="123123", database="test")
con = db.cursor()
# 建立数据表存放ip
sql = 'create table if not exists ips(ip char(20),POTR char(20), 匿名度 char(20),类型 char(20), getpost char(20), 位置 text, 响应速度 char(20), 最后验证 char(20))'
con.execute(sql)
db.commit()
print("操作成功!")
url = "https://www.kuaidaili.com/ops/proxylist/1/"
res = requests.get(url)
res.encoding = "utf-8"
text = res.text
selector = etree.HTML(text)
s = selector.xpath('//*[@id="freelist"]/table')
title = s[0].xpath('./thead/tr/th/text()')
with open('./ips.csv', "a", newline="") as code:
f = csv.writer(code)
f.writerow(title)
print("标题写入完毕")
for f in range(1, 11):
url = "https://www.kuaidaili.com/ops/proxylist/" + str(f) + "/"
res = requests.get(url)
res.encoding = "utf-8"
text = res.text
selector = etree.HTML(text)
s = selector.xpath('//*[@id="freelist"]/table')
for i in range(1, 11):
td = s[0].xpath('./tbody/tr[' + str(i) + ']/td/text()')
proxies = {
"http": td[0] + ":" + td[1]
}
rt = requests.get(url="https://www.baidu.com/", proxies=proxies)
# rt.status_code响应状态码,能够响应的写入文档
if rt.status_code == 200:
sql1 = 'replace into ips values("' + td[0] + '","' + td[1] + '","' + td[2] + '","' + td[3] + '","' + td[4] + '","' + td[5] + '","' + td[6] + '","' + td[7] + '")'
# print(sql1)
con.execute(sql1)
db.commit()
with open('./ips.csv', "a", newline="") as code:
f = csv.writer(code)
f.writerow(td)
print("内容写入完毕! 数据库写入完毕")
except:
print("数据库链接错误.....")
ips()

浙公网安备 33010602011771号