创建手机网站免费,手机平面设计软件app,可做市值曲线的网站,廊坊企业网站服务设计目的
该项目框架Scrapy可以让我们平时所学的技术整合旨在帮助学习者提高Python编程技能并熟悉基本概念#xff1a;
1. 学习基本概念#xff1a;介绍Python的基本概念#xff0c;如变量、数据类型、条件语句、循环等。
2. 掌握基本编程技巧#xff1a;教授学生如何使…设计目的
该项目框架Scrapy可以让我们平时所学的技术整合旨在帮助学习者提高Python编程技能并熟悉基本概念
1. 学习基本概念介绍Python的基本概念如变量、数据类型、条件语句、循环等。
2. 掌握基本编程技巧教授学生如何使用Python编写简单的程序如计算器、字符串处理等。
3. 熟悉Python样式指南学习Python的官方样式指南PEP 8如编码风格、行长限制等以确保代码的可读性和可维护性。
4. 掌握常见数据结构和算法介绍列表、元组、字典、栈、队列等常见数据结构以及相关的算法如排序、搜索等。
5. 学习基于对象的编程教授学生如何使用Python的面向对象编程OOP概念如类、对象、继承、多态等。
6. 掌握网络编程介绍Python网络编程中的基本概念和技术如网络请求、网络服务器、套接字等。
7. 学习多线程和多进程编程教授学生如何使用Python的多线程和多进程编程技术以提高程序性能和可扩展性。
8. 熟悉Python库和模块介绍Python的常用库和模块如from os import remove from time import sleep等以便学生在实际项目中应用Python编程技能。
9. 实践项目通过完成实践项目帮助学生将所学知识应用到实际编程场景提高实际编程能力。
代码审查和优化教授学生如何对自己和他人的Python代码进行审查以提高代码质量和可读性。
10.通过这个课程学生将掌握Python编程的基本技能了解Python的编程风格和最佳实践并学会应用Python编程技能来解决实际问题
11. Scrapy框架介绍介绍Scrapy的主要功能和使用场景以便学习者了解如何使用Scrapy进行爬取。Scrapy的使用步骤详细介绍如何使用Scrapy编写爬虫脚本包括定义爬虫项目、设置爬虫设置、编写爬虫规则和处理爬取结果等。实际爬取案例通过实际爬取案例如爬取广东各城市天气预报帮助学习者了解如何使用Scrapy爬取实际世界中的天气预报数据。处理HTML标签和网页源代码教授学习者如何识别和处理常见HTML标签以及如何理解网页源代码结构。使用Beautiful Soup库介绍Beautiful Soup库的功能并通过实际例子教授学习者如何使用Beautiful Soup从网页中提取数据。
设计内容与要求
1、熟练安装 Python 扩展库 scrapy。
2、熟悉常见 HTML 标签的用法。
3、理解网页源代码结构。
4、理解 scrapy 框架工作原理。实验内容
5.安 装 Python 扩 展 库 scrapy 然 后 编 写 爬 虫 项 目 从 网 站 http://www.weather.com.cn/guangdong/index.shtml 爬取广东各城市的天气预报数据并把爬取到的天气数据写入本地文本 weather.txt。
设计思路
学习使用Scrapy框架爬取天气预报提高Python爬虫技能
在这次课程考核实验中我们将使用Python和Scrapy框架编写一个爬取天气预报的项目。具体实验步骤如下
1. 安装Scrapy首先确保已安装Python扩展库Scrapy。可以通过pip install scrapy命令安装。
2. 创建爬虫项目创建一个新的Python文件例如weather_spider.py并在其中定义爬虫类。在爬虫类中我们将编写代码来爬取天气预报信息。
3. 编写爬虫代码在爬虫类的parse方法中我们将使用Scrapy的API来提取网页中的天气信息。具体实现方法可能因网站的结构和布局而异但通常可以使用XPath或CSS选择器来提取所需的元素。
4. 运行爬虫在命令行中运行爬虫项目并指定爬取的城市和天气预报的时间范围。例如scrapy crawl weather_spider -a start_dates2023-12-15,end_dates2023-12-24 -s OUTPUTfile:///path/to/output.txt.
5. 保存爬取结果爬虫运行完成后将爬取到的天气信息保存到指定的文件中例如output.txt。
通过这个实验您将学习如何使用Python和Scrapy框架爬取天气预报信息。在实际应用中您可以根据需要调整爬虫代码以适应不同的网站结构和布局。同时您还可以尝试使用其他爬虫技术如 Beautiful Soup 和 Selenium以进一步提高爬取效率和灵活性。以下是设计的流程图
实现过程
实验过程和实验步骤如下以及详细的核心代码解释
1.在命令提示符环境使用 pip install scrapy 命令安装 Python 扩展库 scrapy
2.在vscode的终端中使用命令提示符 scrapy startproject GuangDongWeather创建爬虫项目文件
3.进入爬虫项目文件夹然后执行命令 scrapy genspider GuangDongWeatherSpider www.weather.com.cn 创建爬虫程序。
4.使用浏览器打开网址http://www.weather.com.cn/guangdong/index.shtml
5.找到下面位置 广东天气预报 - 广东 6.在页面上单击鼠标右键选择“查看网页源代码”然后找到与“城市预报列表” 对应的位置。 7.选择并打开广东省内任意城市的天气预报页面此处以深圳为例 8.在页面上单击鼠标右键选择“查看网页源代码”找到与上图中天气预报相对应 的位置。 9.修改items.py文件定义要爬取的内容如下
import scrapyclass GuangDongWeatherSpiderItem(scrapy.Item):city scrapy.Field()weather scrapy.Field()# define the fields for your item here like:# name scrapy.Field()pass10.修改爬虫文件 GuangDongWeatherSpider.py定义如何爬取内容其中用到的规则参考前面对页面的分析如果无法正常运行有可能是网页结构有变化可以回到前面的步骤重新分析网页源代码,代码如下
import scrapy
from os import remove
from time import sleep
from GuangDongWeather.items import GuangDongWeatherSpiderItemclass EveryCitySpider(scrapy.Spider):name GuangDongWeatherSpiderallowed_domains [www.weather.com.cn]# 首页爬虫开始工作的页面start_urls [http://www.weather.com.cn/shengzhen/index.shtml]try:remove(lqf.txt)except:passdef parse(self, response):# 获取每个地市的链接地址针对每个链接地址发起请求urls response.css(dta[title]::attr(href)).getall()for url in urls:yield scrapy.Request(urlurl, callbackself.parse_city)sleep(0.3)def parse_city(self, response):处理每个地市天气预报链接地址的实例方法# 用来存储采集到的信息的对象item GuangDongWeatherSpiderItem()# 获取城市名称city response.xpath(//div[classcrumbs fl]/a[3]/text())item[city] city.get()# 定位包含天气预报信息的ul节点其中每个li节点存放一天的天气selector response.xpath(//ul[classt clearfix])[0]weather []# 遍历当前ul节点中的所有li节点提取每天的天气信息for li in selector.xpath(./li):# 提取日期date li.xpath(./h1/text()).get()# 云的情况cloud li.xpath(./p[title]/text()).get()# 晚上不显示高温high li.xpath(./p[classtem]/span/text()).get(none)low li.xpath(./p[classtem]/i/text()).get()wind li.xpath(./p[classwin]/em/span[1]/title).get()wind ,li.xpath(./p[classwin]/i/text()).get()weather.append(f{date}:{cloud},{high}/{low},{wind})item[weather] \n.join(weather)return [item]11.修改 settings.py 文件分派任务指定处理数据的程序如下这里划线的名称要对应你的文件名相当于再找信道在GuangDongWeather这个文件下找pipelines然后再找里面的GuangDongWeathersSpiderPipeline
BOT_NAME GuangDongWeatherSPIDER_MODULES [GuangDongWeather.spiders]
NEWSPIDER_MODULE GuangDongWeather.spiders
ITEM_PIPELINES {
GuangDongWeather.pipelines.GuangDongWeatherSpiderPipeline:1,
}12.修改 pipelines.py 文件把爬取到的数据写入文件 lqf.txt代码如下
from itemadapter import ItemAdapterclass GuangDongWeatherSpiderPipeline(object):def process_item(self, item, spider):with open(lqf.txt, a, encodingutf8) as fp: fp.write(item[city]\n) fp.write(item[weather]\n\n)return item13.最后就可以通过命令去运行框架如下你需要去到对应的目录下运行不然会报错
scrapy crawl GuangDongWeatherSpider 最后需要注意以下几点第一需要注意的就是版本的问题第二框架问题第三电脑问题这个时候你就要多留意一下报错的原因然后代码都要进行分析看看它再说的什么这样子就可以解决大部分问题 文章转载自: http://www.morning.ylsxk.cn.gov.cn.ylsxk.cn http://www.morning.cnprt.cn.gov.cn.cnprt.cn http://www.morning.tnjz.cn.gov.cn.tnjz.cn http://www.morning.dzzjq.cn.gov.cn.dzzjq.cn http://www.morning.tzjqm.cn.gov.cn.tzjqm.cn http://www.morning.lzsxp.cn.gov.cn.lzsxp.cn http://www.morning.sjjq.cn.gov.cn.sjjq.cn http://www.morning.nyjgm.cn.gov.cn.nyjgm.cn http://www.morning.ljbpk.cn.gov.cn.ljbpk.cn http://www.morning.hrpjx.cn.gov.cn.hrpjx.cn http://www.morning.wjndl.cn.gov.cn.wjndl.cn http://www.morning.qichetc.com.gov.cn.qichetc.com http://www.morning.rqnhf.cn.gov.cn.rqnhf.cn http://www.morning.gwqq.cn.gov.cn.gwqq.cn http://www.morning.wjlhp.cn.gov.cn.wjlhp.cn http://www.morning.ryywf.cn.gov.cn.ryywf.cn http://www.morning.xzkgp.cn.gov.cn.xzkgp.cn http://www.morning.qfrsm.cn.gov.cn.qfrsm.cn http://www.morning.xsgxp.cn.gov.cn.xsgxp.cn http://www.morning.hdzty.cn.gov.cn.hdzty.cn http://www.morning.mdxwz.cn.gov.cn.mdxwz.cn http://www.morning.sfwd.cn.gov.cn.sfwd.cn http://www.morning.ddjp.cn.gov.cn.ddjp.cn http://www.morning.mwmxs.cn.gov.cn.mwmxs.cn http://www.morning.mtrrf.cn.gov.cn.mtrrf.cn http://www.morning.drgmr.cn.gov.cn.drgmr.cn http://www.morning.dzgmj.cn.gov.cn.dzgmj.cn http://www.morning.kjjbz.cn.gov.cn.kjjbz.cn http://www.morning.tsqpd.cn.gov.cn.tsqpd.cn http://www.morning.nywrm.cn.gov.cn.nywrm.cn http://www.morning.beiyishengxin.cn.gov.cn.beiyishengxin.cn http://www.morning.xnnxp.cn.gov.cn.xnnxp.cn http://www.morning.trqhd.cn.gov.cn.trqhd.cn http://www.morning.rnyhx.cn.gov.cn.rnyhx.cn http://www.morning.tntbs.cn.gov.cn.tntbs.cn http://www.morning.hbkkc.cn.gov.cn.hbkkc.cn http://www.morning.fqsxf.cn.gov.cn.fqsxf.cn http://www.morning.mrgby.cn.gov.cn.mrgby.cn http://www.morning.srcth.cn.gov.cn.srcth.cn http://www.morning.prhqn.cn.gov.cn.prhqn.cn http://www.morning.zdqsc.cn.gov.cn.zdqsc.cn http://www.morning.mzhjx.cn.gov.cn.mzhjx.cn http://www.morning.tfpmf.cn.gov.cn.tfpmf.cn http://www.morning.smkxm.cn.gov.cn.smkxm.cn http://www.morning.rqjfm.cn.gov.cn.rqjfm.cn http://www.morning.hqllj.cn.gov.cn.hqllj.cn http://www.morning.bqyb.cn.gov.cn.bqyb.cn http://www.morning.mhnrx.cn.gov.cn.mhnrx.cn http://www.morning.mhnb.cn.gov.cn.mhnb.cn http://www.morning.qykxj.cn.gov.cn.qykxj.cn http://www.morning.mnccq.cn.gov.cn.mnccq.cn http://www.morning.byywt.cn.gov.cn.byywt.cn http://www.morning.dqgbx.cn.gov.cn.dqgbx.cn http://www.morning.smkxm.cn.gov.cn.smkxm.cn http://www.morning.ygmw.cn.gov.cn.ygmw.cn http://www.morning.sacxbs.cn.gov.cn.sacxbs.cn http://www.morning.hyyxsc.cn.gov.cn.hyyxsc.cn http://www.morning.wjlhp.cn.gov.cn.wjlhp.cn http://www.morning.kclkb.cn.gov.cn.kclkb.cn http://www.morning.bnzjx.cn.gov.cn.bnzjx.cn http://www.morning.zcwzl.cn.gov.cn.zcwzl.cn http://www.morning.rlhgx.cn.gov.cn.rlhgx.cn http://www.morning.gychx.cn.gov.cn.gychx.cn http://www.morning.nlqgb.cn.gov.cn.nlqgb.cn http://www.morning.dkzrs.cn.gov.cn.dkzrs.cn http://www.morning.jcxqc.cn.gov.cn.jcxqc.cn http://www.morning.yrqb.cn.gov.cn.yrqb.cn http://www.morning.jhgxh.cn.gov.cn.jhgxh.cn http://www.morning.xqbgm.cn.gov.cn.xqbgm.cn http://www.morning.tktcr.cn.gov.cn.tktcr.cn http://www.morning.kjjbz.cn.gov.cn.kjjbz.cn http://www.morning.lbywt.cn.gov.cn.lbywt.cn http://www.morning.lxbml.cn.gov.cn.lxbml.cn http://www.morning.ztnmc.cn.gov.cn.ztnmc.cn http://www.morning.xhhzn.cn.gov.cn.xhhzn.cn http://www.morning.fnnkl.cn.gov.cn.fnnkl.cn http://www.morning.lxdbn.cn.gov.cn.lxdbn.cn http://www.morning.kjgdm.cn.gov.cn.kjgdm.cn http://www.morning.wqngt.cn.gov.cn.wqngt.cn http://www.morning.dpqqg.cn.gov.cn.dpqqg.cn