scrapy抓取博客

scrapy主要的步骤:

  • 创建一个scrapy项目
  • 获取博客内容和保存
  • 抓去url和title
  • 储存标题和链接
  • 获取文章内容

一.创建一个scrapy: 

在桌面上会生成一个文件夹:

在开始爬虫前,打开items.py:

class BlogspiderTtem(scrapy.Item):
    title=scrapy.Field()
    link=scrapy.Field()
    concent=scrapy.Field()

二.获取博客网页并保存:

1. 在blogSpider/spider创建一个eqwaak.py的文件,并制定爬取的范围:
import scrapy

class EqwaakSpider(scrapy.Spider):
    name = 'eqwaak'
    allowed_domains=['链接']
    start_urls=['链接']
    
    
    def parse(self, response):
        pass

创建爬虫器,必须要继承scarpy.Spider类:

  • name:爬虫名字的唯一名字
  • allow_domains=[]:爬取域名下这个网页
  • start_urls:爬虫会在启动爬取url列表
  • parse():爬虫的一个方法,获取爬虫的response,解析网页
 2.修改parse的内容:
    def parse(self, response):
        print(response.text)
        filename="index.html"
        with open(filename,'w',encoding='utf-8')as f:
            f.write(response.text)

然后在cmd里面执行:

scrapy crwal  爬虫名字

三.提取博客标题和链接数据 

 修改eqwaak.py的代码:

import scrapy
from bs4 import BeautifulSoup

class EqwaakSpider(scrapy.Spider):
    name = 'eqwaak'
    allowed_domains=['']
    start_urls=['']


    def parse(self, response):
       soup=BeautifulSoup(response.text,"lxml")
       title_list=soup.find("h4",class_="blog-img-box-top").a.text.strip()
       print('文章标题:',title_list)
       
       for i in range(len(title_list)):
           title=title_list[i].a.text.strip()
           print('第%s篇文章标题是%s'%(i+1,title))
里面把提取的数据放入里面:

 

import scrapy
from bs4 import BeautifulSoup
from financeSpider.items import BlogspiderTtem

class EqwaakSpider(scrapy.Spider):
    name = 'eqwaak'
    allowed_domains=['链接']
    start_urls=['链接']


    def parse(self, response):
       items=[]
       soup=BeautifulSoup(response.text,"lxml")
       title_list=soup.find("h4",class_="blog-img-box-top")
       print('文章标题:',title_list)

       for i in range(len(title_list)):
           item=BlogspiderTtem()
           title=title_list[i].a.text.strip()
           link=title_list[i].a["herf"]
           
           item["title"]=title
           item["link"]=link
           items.append(item)
           
        return items
如果输出是josn格式,可以写:

scrapy crwal eqwaak -o article.json 

 如果输出是csv格式,可以写:

scrapy crwal eqwaak -o artticle.vsc 

四.储存博客标题和链接 :

 打开pipelines.py,代码:
class FinancespiderPipeline(object):
    #保存地址
    file_path="C:/Users/xuyix/Desktop/financeSpider/result.txt"

    def __init__(self):
        self.article=open(self.file_path,"a+",encoding="utf-8")

    def process_item(self, item, spider):
        title=item["title"]
        link=item["link"]
        content=item["content"]
        output=title+'\t'+link+'\t'+content+'\n\n'
        self.article.write(output)
        return item
还需要修改setting里面的代码
取消注释
ITEM_PIPELINES = {
   "financeSpider.pipelines.FinancespiderPipeline": 300,
}
在命令行输入: 

scrapy crwal eqwaak 

五.获取文章内容: 

在eqwaak.py里面添加代码:
    def parse2(self,response):
        item=response.meta['item']
        #解析文章内容
        soup=BeautifulSoup(response.text,"lxml")
        content=soup.find("div",class_="blog-img-box-top").text.strip()
        content=content.replace("\n","")
        item["content"]=content
        yield item
删除text文件:

scrapy crwal eqwaak

  • 14
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

eqwaak0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值