4

Python爬虫编程思想(156):使用Scrapy抓取天气预报数据

 2 years ago
source link: https://blog.csdn.net/nokiaguy/article/details/126334951
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Python爬虫编程思想(156):使用Scrapy抓取天气预报数据

        并不是所有的数据都在网页代码中,对于通过AJAX方式更新数据的Web页面,通常会使用Web API的方式从服务端获取数据,然后通过JavaScript代码将这些数据显示在Web页面的组件中。在这种情况下,无法通过抓取HTML代码的方式获取这些数据,而要通过直接访问这些Web API的方式从服务端抓取数据。

        Web API返回数据的形式有多种,但通常是以JSON格式的数据返回,当然,有可能会在返回的数据中插入其他的东西,例如JavaScript代码、HTML代码、CSS等。这就要具体问题具体分析了。对于这种情况,是无法编写通用的爬虫程序的,需要针对特定的Web API进行分析和抓取。

        本例分析了http://www.weather.com.cn网站的Web API数据格式,并模拟浏览器访问该网站的Web API,通过该网站的Web API可以获取指定城市的天气预报数据。本例不仅详细描述的实现过程,还深入分析了如何从Web页面中找出Web API Url。

        抓取Web API数据不像抓取Web页面数据那么直接,抓取Web页面时已经得知了该页面的Url,所以只需要传给爬虫该Url,就可以下载该页面的代码,然后通

文章知识点与官方知识档案匹配,可进一步学习相关知识

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK