大家好,感谢邀请,今天来为大家分享一下作业解答网站源码分享的问题,以及和作业免费解答的一些困惑,大家要是还不太明白的话,也没有关系,因为接下来将为大家分享,希望可以帮助到大家,解决大家的问题,下面就开始吧!
今天我们来介绍和实战Scrapy框架的命令行模式,它和Django框架的shell模式一样,用于我们前期调试工程代码,非常方便,掌握好Scrapy的shell模式会使得我们开发爬虫更为顺畅。
1.ScrapyShell介绍
ScrapyShell*是*一个交互终端,类似于Python交互式模式,它使我们可以在未启动Scrapy爬虫的情况下调试爬虫代码。
在Scrapy的交互模式下,我们可以直接获取网页的Response结果,然后使用XPath或CSS表达式来获取网页元素,并以此测试我们获取网页数据的Xpath或者CSS表达式,确保后续执行时能正确得到数据。我们来看看如何进入shell模式,
在Scrapy框架中内置了Selector选择器,这个选择器是属于parsel模块的,而parsel模块是由Scrapy团队为解析网页而开发的,并且独立出来形成了一个第三方模块。
这样我们可以在自己的爬虫程序中使用parsel模块而不必基于Scrapy框架。我们从源码中来看看这个选择器类的定义。可以看到Selector类有我们熟悉的xpath()、css()、re()以及extract()等方法,这些是我们解析网页的基础。
Selector类方法
我们来思考一个问题,然后去源码中找到答案:
(scrapy-test)[root@server~]39;scrapy.http.response.html.HtmlResponse&源码位置:scrapy/http/response/html.py\nfromscrapy.http.response.textimportTextResponse\n\nclassHtmlResponse(TextResponse):\npass\n代码块12345
这个够不够简单?继续追看TextResponse的定义:
…\n\nclassTextResponse(Response):\n…\n\ndefxpath(self,query,**kwargs):\nreturnself.selector.xpath(query,**kwargs)\n\ndefcss(self,query):\nreturnself.selector.css(query)\n\n源码位置:scrapy/selector/unified.py\n\nfromparselimportSelectoras_ParselSelector\n\nclassSelector(_ParselSelector,object_ref):\nscrapyshellhttps://gz.lianjia.com/ershoufang/\n…\n>>>response\n<200https://gz.lianjia.com/ershoufang/>\n代码块1234
我们看到响应的网页数据已经有了,接下来我们就可以开始进行网页分析来抓取图片中标记的数据了。首先是标题信息:
提取二手房数据的标题信息
根据上面的网页结构,可以很快得到标题的xpath路径表达式:
标题://ul[@class=&34;]/li/div/div[@class=&34;]/a/text()\n代码块1
在ScrapyShell中我们实战一把:
>>>response.xpath(&34;sellListContent&34;title&39;)\n[<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>,<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>,<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>,<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>,<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>,<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>,<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>,<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>,<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>,<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>,<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>,<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>,<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>,<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>,<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>,<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>,<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>,<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>,<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>,<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>,<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>,<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>,<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>,<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>,<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>,<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>,<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>,<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>,<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>,<Selectorxpath=&34;sellListContent&34;title&39;data=&39;>]\n代码块12
上面结果返回的是SelectorList类型:
>>>data_list=response.xpath(&34;sellListContent&34;title&39;)\n>>>type(data_list)\n<class&39;>\n代码块123
最后我们通过提取Selector的root属性,只得到相应的文本信息:
>>>data=[d.rootfordindata_list]\n>>>data\n[&39;,&39;,&39;,&39;,&39;,&39;,&39;,&39;,&39;,&39;,&39;,&39;,&39;,&39;,&39;,&39;,&39;,&39;,&39;,&39;,&39;,&39;,&39;,&39;,&39;,&39;,&39;,&39;,&39;,&39;]\n代码块123
是不是非常简单就爬到了数据?另外,我们还可以使用extract()[0]或者extract_first()这样的方式来提取结果列表中的第一个文本数据:
>>>data=response.xpath(&34;sellListContent&34;title&39;).extract()[0]\n>>>data2=response.xpath(&34;sellListContent&34;title&39;).extract_first()\n>>>data==data2\nTrue\n>>>data\n&39;\n代码块123456
接下来,我们依次找出获取二手房位置、房屋价格、房屋信息的xpath路径表达式:
房屋位置:\n//ul[@class=&34;]/li/div/div[@class=&34;]/div[@class=&34;]/a[]/text()\n房屋信息:\n//ul[@class=&34;]/li/div/div[@class=&34;]/div[@class=&34;]/text()\n房屋价格:\n//ul[@class=&34;]/li/div/div[@class=&34;]/div[@class=&34;]/span/text()\n代码块123456
有了这些之后,我们就可以依次提取出二手房的【标题介绍】、【房屋位置】、【房屋信息】以及【房屋价格】这些信息。此外对于提取的【房屋信息】字段还要进一步处理,分割成【房屋结构】、【房屋大小】以及【朝向】等信息。这些信息将在Spider模块中进行提取,也就是我们前面互动出版网爬虫的ChinaPubCrawler.py文件中的ChinaPubCrawler类来解析。
最后我们在介绍下scrapyshell命令的参数:
(scrapy-test)[root@server~]#scrapyshell–help\nUsage\n=====\nscrapyshell[url|file]\n\nInteractiveconsoleforscrapingthegivenurlorfile.Use./file.htmlsyntax\norfullpathforlocalfile.\n\nOptions\n=======\n–help,-hshowthishelpmessageandexit\n-cCODEevaluatethecodeintheshell,printtheresultand\nexit\n–spider=SPIDERusethisspider\n–no-redirectdonothandleHTTP3xxstatuscodesandprintresponse\nas-is\n\nGlobalOptions\n————–\n–logfile=FILElogfile.ifomittedstderrwillbeused\n–loglevel=LEVEL,-LLEVEL\nloglevel(default:DEBUG)\n–nologdisableloggingcompletely\n–profile=FILEwritepythoncProfilestatstoFILE\n–pidfile=FILEwriteprocessIDtoFILE\n–set=NAME=VALUE,-sNAME=VALUE\nset/overridesetting(mayberepeated)\n–pdbenablepdbonfailure\n
比较常用的有–no-redirect和-s选项:
–no-redirect:指的是不处理重定向,直接按照原始响应返回即可;-s:替换settings.py中的配置。常用的有设置USER_AGENT等。
3.小结
本小节中我们简单介绍了Scrapy的Shell模式以及Response类的一些原理。紧接着我们开始了ScrapyShell实战,以广州链家网为例,测试在Shell模式下使用Selector选择器来解析网页数据,提取房屋标题、位置、价格等数据。下面一节我们会深入分析Scrapy框架中的Request和Response,包括解答今天的一个源码追踪作业。
文章到此结束,如果本次分享的作业解答网站源码分享和作业免费解答的问题解决了您的问题,那么我们由衷的感到高兴!
