网页插入 全国各城市天气代码

现在各大论坛或者博客中都会有天气预报,在做项目的时候,很多时候我们都会使用解析XML文件的方法获得全国各个城市的天气情况,然后插入网页。这样很麻烦,因为我们还必须自己判断用户的IP,获取用户所在的城市,然后查找改城市的代号,然后获取XML文件进行解析。今天在网上看了一下找到了很好用的天气插件,只需要把下面的代码加入你的网页就可以显示天气预报,插件会自动判断用户的所在地,返回正确的天气情况,下面就给大家分享一下,以后写项目,如果没有特殊的天气要求,使用下面的插件很方便的。

插件1:

网页代码:

<iframe src="http://m.weather.com.cn/m/pn1/weather.htm " width="235" height="20" marginwidth="0" 
marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no"></iframe>

效果:

weather1

插件2:

网页代码:

<iframe src="http://m.weather.com.cn/m/pn2/weather.htm " width="180" height="20" marginwidth="0" 
marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no"></iframe>

效果:

weather2

插件3:

网页代码:

<iframe src="http://m.weather.com.cn/m/pn3/weather.htm " width="225" height="20" marginwidth="0" 
marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no"></iframe>

效果:

weather3

插件4:

网页代码:

<iframe src="http://m.weather.com.cn/m/pn4/weather.htm " width="160" height="20" marginwidth="0" 
marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no"></iframe>

效果:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要获取城市天气网页上的温湿度数据并插入数据库,你可以使用Python的requests和BeautifulSoup库来解析网页。具体步骤如下: 1. 安装requests和BeautifulSoup库 ``` pip install requests pip install beautifulsoup4 ``` 2. 发送HTTP请求获取网页内容 ``` import requests url = 'http://www.weather.com.cn/weather/101010100.shtml' # 以北京天气网页为例 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} response = requests.get(url, headers=headers) ``` 3. 使用BeautifulSoup解析网页内容 ``` from bs4 import BeautifulSoup soup = BeautifulSoup(response.text, 'html.parser') temp = soup.select('.tem i')[0].get_text() # 获取温度 humidity = soup.select('.shidu li b')[0].get_text() # 获取湿度 ``` 4. 将数据插入数据库 ``` import pymysql # 连接数据库 conn = pymysql.connect(host='localhost', user='root', password='password', database='weather', charset='utf8') cursor = conn.cursor() # 插入数据 sql = 'insert into weather(city, temperature, humidity) values(%s, %s, %s)' cursor.execute(sql, ('北京', temp, humidity)) # 提交事务并关闭连接 conn.commit() cursor.close() conn.close() ``` 完整代码: ``` import requests from bs4 import BeautifulSoup import pymysql # 发送HTTP请求获取网页内容 url = 'http://www.weather.com.cn/weather/101010100.shtml' headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} response = requests.get(url, headers=headers) # 使用BeautifulSoup解析网页内容 soup = BeautifulSoup(response.text, 'html.parser') temp = soup.select('.tem i')[0].get_text() # 获取温度 humidity = soup.select('.shidu li b')[0].get_text() # 获取湿度 # 将数据插入数据库 conn = pymysql.connect(host='localhost', user='root', password='password', database='weather', charset='utf8') cursor = conn.cursor() sql = 'insert into weather(city, temperature, humidity) values(%s, %s, %s)' cursor.execute(sql, ('北京', temp, humidity)) conn.commit() cursor.close() conn.close() ``` 这个示例代码插入一条数据到名为 weather 的数据库表,如果你没有该表,则需要在你的数据库创建一个名为 weather 的表,并且该表应该至少包含三个字段:city、temperature和humidity。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值