scrapy爬虫实战(部分源代码)

items.py

python 复制代码
# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html

import scrapy


class Sss1Item(scrapy.Item):
    # define the fields for your item here like:
    name = scrapy.Field()

spider_title.py

python 复制代码
import scrapy
from sss1.items import Sss1Item
class SpiderTitleSpider(scrapy.Spider):
    name = "spider_title"
    allowed_domains = ["www.zongheng.com"]
    start_urls = ["https://read.zongheng.com/chapter/1215341/68208370.html"]

    def parse(self, response):
        item=Sss1Item()
        f=open('我有一剑.txt','a',encoding='utf8')
        titles=response.xpath('//*[@id="Jcontent"]/div/div[1]/div[2]/text()').extract()
        for asd in titles:
            f.write(asd+"\n")

        names=[each.extract() for each in response.xpath('//*[@id="Jcontent"]/div/div[4]/p[3]/span[1]/text()')]
        # for asd in names:
        #     f.write(asd+"\n")
        item['name']=names
        yield item
        next=response.xpath('//*[@id="page_reader"]/div[3]/div[1]/div[3]/div[1]/a[3]/@href').get()
        next=next.replace("?","")
        print('----------------------------------------------------------')
        print(next)
        if next:
            yield scrapy.Request(url=next,callback=self.parse)
相关推荐
Caco.D9 小时前
Aneiang.Pa.News:属于你自己的全平台热点聚合阅读器
爬虫·asp.net·aneiang.pa·热榜新闻
@杨某15 小时前
scrapy的暂停与重启
scrapy
小白学大数据16 小时前
Java 异步爬虫高效获取小红书短视频内容
java·开发语言·爬虫·python·音视频
我想吃烤肉肉16 小时前
Python 中 asyncio 是什么?
爬虫·python·自动化
@杨某21 小时前
超级鹰的使用
爬虫·selenium
小白学大数据21 小时前
百科词条结构化抓取:Java 正则表达式与 XPath 解析对比
java·开发语言·爬虫·正则表达式
泡泡以安1 天前
【爬虫教程】第4章:HTTP客户端库深度定制(httpx/aiohttp)
爬虫·http·httpx
@杨某1 天前
selenium嵌入scrapy动态网页抓取
selenium·测试工具·scrapy
搞全栈小苏1 天前
【Python 爬虫实战】一键批量下载 Python 官网 FTP 所有版本全部文件(多线程 + 断点续传 + 禁用系统代理 完整版)
开发语言·爬虫·python
泡泡以安1 天前
【爬虫教程】第5章:WebSocket协议解析与长连接管理
爬虫·websocket·网络协议