输入scrapy crawl zhilian_spider然后运行,zhilian_spider的值为继承Spider的类的name的值
这个的后半段是解释前面运行的含义吗?
参考GPT和自己的思路:
对的,输入"scrapy crawl zhilian_spider"这个命令是用来启动一个名为"zhilian_spider"的爬虫程序,程序的实现需要继承自Scrapy中的Spider类,所以"zhilian_spider"的值就是该类的name属性的值。这个命令的运行会启动该爬虫程序对指定网站信息进行抓取,并根据程序中的逻辑进行数据提取和处理。
智联爬虫?从字面上看是这个
后面的是参数
好笑的一幕来了,我的前后两个机器人程序,一个说对的,一个说不是,哈哈。
该回答引用GPTᴼᴾᴱᴺᴬᴵ
不是,这句话的含义是执行 Scrapy 框架中的命令 scrapy crawl zhilian_spider,其中 zhilian_spider 是继承 Spider 类的类的 name 属性的值。在 Scrapy 中,可以通过继承 scrapy.Spider 类来编写一个 Spider(爬虫)程序,然后使用 scrapy crawl 命令来启动该爬虫程序。在这个命令中, 参数就是指定 Spider 类的 name 属性的值,用于标识该爬虫程序的名称。
import scrapy
from scrapy import Request
from scrapy.spiders import Spider
from ..items import ShicimingjuItem
class GssSpiderSpider(Spider):
name = 'scmj_spider' # 爬虫名称
def start_requests(self):#发起起始页的请求
urls ='https://www.shicimingju.com/shicimark/aiqingshi.html'
yield Request(urls)
#解析内容
def parse(self, response):
scmj_divs = response.xpath("//div[@class='card shici_card']/div")
for scmj_divs in scmj_divs:
#获取诗名
title = scmj_divs.xpath('div[2]/h3/a/text()').get()
print("标题:", title)
# 获取诗的朝代和作者
source = scmj_divs.xpath("div/text()")
dynasty=str()
author=str()
if source.getall():
dynasty=source.getall()[1]
dynasty=dynasty.strip()
author=source.getall()[2]
author=author.strip()
print("朝代:",dynasty)
print("作者:",author)
# 获取诗的具体内容
content_list = scmj_divs.xpath("div[2]/div/text()")
content=str()
if content_list.getall():
content=content_list.getall()[0:2]
#转换成字符串,并给每一句诗句添加换行符,去掉空格
content="\n".join(content).strip()
#解析网页中需要点击更多显示的诗句
content_part = scmj_divs.xpath("div[2]/div/div/text()")
if content_part.getall():
content_part=content_part.getall()[0:-1]
#转换成字符串,并给每一句诗句添加换行符,去掉空格
content_part="\n".join(content_part).strip()
#拼接两部分诗句为完整诗句
content=content+"\n"+content_part
print(content)
# 将解析的数据传入item中
item = ShicimingjuItem(title=title, dynasty=dynasty, author=author, content=content)
# 将item传送到管道piplines
yield item
# 多页面爬取
#解析下一页链接中url,因为第二页开始下一页的标签位置改变,需要判断后再进行解析
next= response.xpath("//div[@id='list_nav_part']/a[8]/text()").get()
if next=="下一页":
next_href = response.xpath("//div[@id='list_nav_part']/a[8]/@href").get()
else:
next_href = response.xpath("//div[@id='list_nav_part']/a[9]/@href").get()
if next_href:
#使用urljoin将下一页url转换成完整url
next_url = response.urljoin(next_href)
print(next_url)
#获取下一页的请求
request = scrapy.Request(next_url)
yield request