import json,requests
from urllib.request import urlopen
from pyquery import PyQuery as pq
from lxml import etree as et
def getIp(): #获取本地网络ip
html_text = et.HTML(requests.get("https://ip.cn/").text)
ip = html_text.xpath('//span/text()')[5].replace(':','').replace(' ','')
print(ip)
return ip
def get_ip_area(ip): #通过ip获取地理位置
try:
apiurl = "http://ip.taobao.com/service/getIpInfo.php?ip=%s" % ip
content = urlopen(apiurl).read()
data = json.loads(content)['data']
code = json.loads(content)['code']
if code == 0: # success
print(data['country_id'])
print(data['area'])
print(data['city'])
print(data['region'])
else:
print(data)
return data
except Exception as ex:
print(ex)
def weather(): #获取本地天气
res = json.loads(pq('https://www.toutiao.com/stream/widget/local_weather/data/?city=%s'%city).text())
local_weather=res['data']['weather']
return local_weather
data里面有四个参数,国家id,国家简称,本地,本地所属的省会。local_weather获取了本地所有的天气,可自己选取所要用的拿出来用,将这几个函数封装好,以便以后须用。