Python抓取天气信息并存储原来这么简单
2019-04-12 09:32:06来源:博客园 阅读 ()

我们计划抓取的数据:杭州的天气信息
实现数据抓取的逻辑:使用python 请求 URL,会返回对应的 HTML 信息,我们解析 html,获得自己需要的数据。(很简单的逻辑)
第一步:创建 Python 文件

写第一段Python代码

这段代码类似于 Java 中的 Main 方法。可以直接鼠标右键,选择 Run。

第二步:请求RUL
python 的强大之处就在于它有大量的模块(类似于Java 的 jar 包)可以直接拿来使用。
我们需要安装一个 request 模块: File - Setting - Product - Product Interpreter


点击如上图的 + 号,就可以安装 Python 模块了。搜索 requests 模块(有 s 噢),点击 Install。

我们顺便再安装一个beautifulSoup4 和 pymysql模块,beautifulSoup4 模块是用来解析 html 的,可以对象化 HTML 字符串。pymysql 模块是用来连接 mysql 数据库使用的。


相关的模块都安装之后,就可以开心的敲代码了。
定义一个 getContent 方法:

在 main 方法中调用:

第三步:分析页面数据
定义一个 getData 方法:

上面的解析其实就是按照 HTML 的规则解析的。可以打开杭州天气在开发者模式中(F12),看一下页面的元素分布。

在 main 方法中调用:

数据写入excel
现在我们已经在 Python 中拿到了想要的数据,对于这些数据我们可以先存放起来,比如把数据写入 csv 中。
定义一个 writeDate 方法:

在 main 方法中调用:

执行之后呢,再指定路径下就会多出一个 weather.csv文件,可以打开看一下内容。


到这里最简单的数据抓取--储存就完成了。
原文链接:https://www.cnblogs.com/Pythonmiss/p/10695809.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:while else
- python3基础之“术语表(2)” 2019-08-13
- python3 之 字符串编码小结(Unicode、utf-8、gbk、gb2312等 2019-08-13
- Python3安装impala 2019-08-13
- 小白如何入门 Python 爬虫? 2019-08-13
- python_字符串方法 2019-08-13
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash