Python 使用scrapy框架

1、安装scrapy

2、使用scrapy创建项目,在终端命令行 执行如下命令,会创建一个myproject项目

scrapy startproject myproject

3、创建完成后,目录结构如下

4、cd myproject进入项目 ,执行scrapy genspider weather  ******,会在spiders下创建一个ptyhon文件

scrapy genspider weather  weather*****

5、这个文件里面就可以写具体的爬虫逻辑了,

import scrapy
import re
from myproject.items import MyprojectItem


class WeatherSpider(scrapy.Spider):
    # 名称
    name = "weather"
    # 爬取域名的范围
    allowed_domains = ["******"]
    # 爬取的网址
    start_urls = ["********"]
    #start_urls = ["********"]

    
    def parse(self, response, **kwargs):
        data = response.xpath("//div[@class='content_l']/dl")
        for each in data:
            # 图片
            img = each.xpath("./dt/a/img/@src").get()
            # 标题
            title = each.xpath("./dd/h3/a/text()").get()
            # 时间
            create_time = each.xpath("./dd/h3/span/text()").get()
            # 简介
            description = each.xpath("./dd/p/text()").get()
            content_href = each.xpath("./dd/h3/a/@href").get()
            # # 内容链接
            # item = MyprojectItem(
            #     img=img,
            #     title=title,
            #     create_time=create_time,
            #     description=description,
            #     content_href=content_href
            # )
            #
            # yield item
            # 定义一个回调函数来处理每个链接的响应
            yield scrapy.Request(url=content_href, callback=self.parse_content_page,
                                      meta={'item': {'img': img, 'title': title, 'create_time': create_time,
                                                     'description': description}})

    def parse_content_page(self, response):
        # print(response.url)
        # response.meta['item'].copy()
        element = response.xpath('//div[@class="articleBody"]').get()
        content = re.search(r'<div class="articleBody">(.*?)</div>', element, re.DOTALL).group(1)
        # 使用正则表达式去除注释内容
        content = re.sub(r'<!--.*?-->', '', content)
        item_data = response.meta['item']
        # print(item_data)
        # 创建item并填充数据
        item = MyprojectItem(
            img=item_data['img'],
            title=item_data['title'],
            create_time=item_data['create_time'],
            description=item_data['description'],
            content=content  # 假设这里添加了从页面中提取的内容
        )

        # Yield填充完毕的item
        yield item

相关推荐

  1. python爬虫框架Scrapy

    2024-06-10 07:32:02       62 阅读
  2. Python爬虫---scrapy框架---下载嵌套数据

    2024-06-10 07:32:02       42 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-10 07:32:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-10 07:32:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-10 07:32:02       82 阅读
  4. Python语言-面向对象

    2024-06-10 07:32:02       91 阅读

热门阅读

  1. 设备安装施工的一点总结

    2024-06-10 07:32:02       28 阅读
  2. conda常见命令

    2024-06-10 07:32:02       27 阅读
  3. Elasticsearch 详细介绍和经典应用

    2024-06-10 07:32:02       29 阅读
  4. 【数据结构】队列的应用(详解)

    2024-06-10 07:32:02       33 阅读
  5. 使用Spring Boot实现Redis多数据库缓存

    2024-06-10 07:32:02       30 阅读
  6. 小米测开面经

    2024-06-10 07:32:02       31 阅读
  7. 正态分布公式

    2024-06-10 07:32:02       33 阅读
  8. 使用 AES 算法在 C# 中实现安全字符串加密和解密

    2024-06-10 07:32:02       28 阅读