-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetIP.py
52 lines (42 loc) · 1.17 KB
/
getIP.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import requests
import time
from bs4 import BeautifulSoup
import re
import sqlite3
agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.20 (KHTML, like Gecko) Chrome/11.0.672.2 Safari/534.20'
headers = {
# "Host": "www.xicidaili.com",
# "Referer": "https://www.xicidaili.com/",
'User-Agent': agent
}
url="http://www.xicidaili.com/"
session=requests.Session()
homeresponse=session.get(url=url, headers=headers)
soup=BeautifulSoup(homeresponse.text,'html.parser')
td=soup.findAll('tr', {'class': 'odd'}).__str__()
i=0
td2=[]
now = time.strftime("%Y-%m-%d") + ".db"
try:
conn = sqlite3.connect(now)
except:
print("Error to open database")
create_tb = '''
CREATE TABLE IF NOT EXISTS PROXY
(
IP TEXT,
PORT TEXT
);
'''
conn.execute(create_tb)
while i<=100:
td2.append(re.findall('<td>(.*?)</td>',td)[i])
td2.append(re.findall('<td>(.*?)</td>',td)[i+1])
i=i+6
insert_db_cmd = '''
INSERT INTO PROXY (IP,PORT) VALUES ('%s','%s');
''' % (re.findall('<td>(.*?)</td>',td)[i], re.findall('<td>(.*?)</td>',td)[i+1])
conn.execute(insert_db_cmd)
conn.commit() # 记得commit
print(td2)
conn.close()