网站改版建设方案,网上创建公司,东莞网站建设实例推荐,公司网站建设服务机构一、实现样式 二、核心点
1、语言#xff1a;Python、HTML#xff0c;CSS 2、python web框架 Flask 3、三方库#xff1a;requests、xpath 4、爬取网站#xff1a;https://ip138.com/ 5、文档结构
三、代码
ipquery.py
import requests
from lxml import etree
# 请求…一、实现样式 二、核心点
1、语言Python、HTMLCSS 2、python web框架 Flask 3、三方库requests、xpath 4、爬取网站https://ip138.com/ 5、文档结构
三、代码
ipquery.py
import requests
from lxml import etree
# 请求user-agent
headers {User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36}def getIpInfo(ip):ipurl fhttps://ip138.com/iplookup.php?ip{ip}action2res requests.get(ipurl,headers headers)e etree.HTML(res.text)ip e.xpath(//div[classcaption]//h1//text())ipinfo e.xpath(//div[classtable-box]//tbody//tr//td[2]//text())ipinfo.append(ip[0])return ipinfo
searchWeather.py
import requests
from lxml import etree,html
import re# 请求user-agent
headers {User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36}
def getWeather(location):weatherUrl fhttps://www.wentian123.com/search/?location{location}res requests.get(weatherUrl,headers headers)e etree.HTML(res.text)weather e.xpath(//table//tbody//td//text())#处理数据# 去除\nweathernew [x.strip() for x in weather]# 去掉空字符串while in weathernew:weathernew.remove()# print(weathernew)# 数据分组 6个一组weatherInfo [weathernew[i:i6] for i in range(0,len(weathernew),6)]return weatherInfoquery.py
from flask import Flask,render_template,requestfrom ipquery.ipquery import getIpInfo
from searchWeather.seachWeather import getWeather
app Flask(__name__)# 主页
app.route(/)
def index():return render_template(index.html,weather [],ipinfo [])# ip地址查询
app.route(/ipquery)
def ipquery():ip request.args.get(ip)ipinfo getIpInfo(ip)# print(ipinfo[1])return render_template(index.html,ipinfo ipinfo,weather [])# 天气查询
app.route(/weather)
def getweather():location request.args.get(location)# print(location)weather getWeather(location)# print(weather)return render_template(index.html,weather weather,ipinfo [])if __name__ __main__:app.run(port5000, debugTrue)
index.html
!DOCTYPE html
html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0!-- 最新版本的 Bootstrap 核心 CSS 文件 --link relstylesheet hrefhttps://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css integritysha384-HSMxcRTRxnNBdg0JdbxYKrThecOKuH5zCYotlSAcp1c8xmyTe9GYg1l9a69psu crossoriginanonymoustitle查询网站/title
/head
style* {margin: 0 auto;padding: 0 auto;/* display: flex; */}.public {box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);margin: 0 auto;width: fit-content;padding: 40px 60px;text-align: center;margin-top: 20px;}
/stylebody stylemargin: 0 auto;!-- ip查询 --divdiv classpublich3IP归属地查询/h3/h3/h3form classform-inline action/ipquerydiv classform-grouplabel forexampleInputEmail2IP地址/labelinput typetext classform-control nameip placeholder请输入需要查询的IP地址 stylewidth: 300px;/divbutton typesubmit classbtn btn-primary查询/button/formtable classtable table-bordered stylewidth: 100%;margin-top: 20px;trth styletext-align: center;IP地址/thth styletext-align: center;ASN归属地/thth styletext-align: center;运营商/th/tr{% if ipinfo ! [] %}trtd{{ipinfo[-1]}}/tdtd{{ipinfo[0]}}/tdtd{{ipinfo[1]}}/td/tr{% endif %}/table/div!-- 天气查询 --div classpublicform classform-inline action/weatherdiv classform-groupinput typetext classform-control namelocation placeholder请输入市区县名称、区号或者邮政 stylewidth: 300px;/divbutton typesubmit classbtn btn-primary查询/button/formtable classtable table-bordered table-striped table-hover stylewidth: 100%;margin-top: 20px;{% if weather ! [] %}tbody{% for i in weather %}trtd colspan4 stylefont-size: 16px;{{i[0]}}/td/trtrtd{{i[1]}}{{i[2]}}/td!-- td{{i[2]}}/td --td{{i[3]}}/tdtd{{i[4]}}/tdtd{{i[5]}}/td/tr{% endfor %}/tbody{% endif %}/div/div
/body
/html四、总结
初学python很多代码写的很冗余不够简洁还有一些逻辑没有处理好感谢贵网站 信息查询网站 提供的接口数据里面还有很多类型的查询这边就简单写了两个查询方法的爬虫继续加油