forked from pangxiaobin/proxy_ip_pool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.py
More file actions
63 lines (54 loc) · 1.65 KB
/
helper.py
File metadata and controls
63 lines (54 loc) · 1.65 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 19-5-15 下午4:54
# @Author : Hubery
# @File : helper.py
# @Software: PyCharm
import json
import requests
from requests.exceptions import ConnectionError
from ProxyIPPool import settings
def get_text(url, options={}):
"""
抓取代理
:param method: 请求方法
:param url: 请求的目标url
:param options:
:return:
"""
headers = dict(settings.BASE_HEADERS, **options)
print('正在抓取', url)
try:
res = requests.get(url, headers=headers, timeout=5)
if res.status_code == 200:
print('抓取成功', url, res.status_code)
return res.text
except ConnectionError:
print('抓取失败', url)
return None
def get_ip_address(ip):
"""
获取ip地址
:param ip:
:return:
"""
url = 'http://ip.taobao.com//service/getIpInfo.php?ip={}'.format(ip)
headers = settings.BASE_HEADERS
try:
res = requests.get(url, headers=headers, timeout=5)
if res.status_code == 200:
res_dict_data = json.loads(res.text).get('data', '')
country = res_dict_data.get('country', '')
region = res_dict_data.get('region', '')
city = res_dict_data.get('city', '')
isp = res_dict_data.get('isp', '')
ip_address = '/'.join([country, region, city, isp])
return ip_address
else:
return ''
except Exception as e:
print('请求淘宝地址失败, 失败失败原因{}'.format(e))
return None
if __name__ == '__main__':
ip_address = get_ip_address('49.5.10.34')
print(ip_address)