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)
相关推荐
wdxylb5 小时前
Pyhton爬虫使用Selenium实现浏览器自动化操作抓取网页
爬虫·selenium·测试工具
菜鸡中的奋斗鸡→挣扎鸡21 小时前
初始爬虫11
开发语言·爬虫·python
凡人的AI工具箱1 天前
15分钟学 Python 第35天 :Python 爬虫入门(一)
开发语言·数据结构·人工智能·后端·爬虫·python
新缸中之脑1 天前
ScrapeGraphAI 大模型增强的网络爬虫
爬虫
易辰君1 天前
python爬虫 - 初识爬虫
开发语言·爬虫·python
人生の三重奏1 天前
爬虫——同步与异步加载
爬虫·jsonpath·同步与异步·腾讯新闻
人生の三重奏1 天前
爬虫——爬取小音乐网站
爬虫
能摆一天是一天2 天前
Python 爬虫 根据ID获得UP视频信息
开发语言·爬虫·python·selenium
麦麦大数据2 天前
scrapy爬取汽车、车评数据【上】
scrapy·汽车
NPE~3 天前
爬虫入门 & Selenium使用
爬虫·python·selenium·测试工具·xpath