写一个真正意义上一个爬虫,并将他爬取到的数据分别保存到txt、json、已经存在的mysql数据库中。
这次我们要爬的是 中国天气网:http://www.weather.com.cn/ 随便点开一个城市的天气比如合肥: http://www.weather.com.cn/weather/101220101.shtml 我们要爬取的就是图中的:合肥七天的前期预报:
我们使用chrome开发者工具,模拟鼠标定位到相对应位置:
可以看到我们需要的数据,全都包裹在
<ul class="t clearfix">
里 我们用bs4、xpath、css之类的选择器定位到这里,再筛选数据就行。 本着学习新知识的原则,文中的代码将会使用xpath定位。 这里我们可以这样:
response.xpath('//ul[@class="t clearfix"]')
json格式:
数据库格式:
这次的例子就到这里了,主要介绍如何通过自定义PIPELINE来将爬取的数据以不同的方式保存。