import scrapy
from scrapy import FormRequest
import json
from items import MyspiderItem


class JianshekuSpider(scrapy.Spider):
    name = 'jianzhu'
    allowed_domains = ['admin.jzda001.com']
    start_url = 'https://admin.jzda001.com/api/core/002--newsList'

    def __init__(self):
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
            "Referer": "hhttps://www.baidu.com/xyzg/"
        }

    # 需要重写 start_requests() 方法

    def start_requests(self):
        # 只获取 20 页数据
        for page in range(1, 5):
            form_data = {
                "type": "1",
                "limit": "17",
                "pageNo": str(page)
            }

            request = FormRequest(
                self.start_url, headers=self.headers, formdata=form_data, callback=self.parse)
            yield request

    def parse(self, response):
        # print(response.body)
        # print(response.url)
        # print(response.body_as_unicode())
        data = json.loads(response.body_as_unicode())
        data = data["rows"] # 获取数据
        print(data)
        for item in data:
            school = MyspiderItem()
            school["title"] = item["title"]
            school["userName"] = item["userName"]
            school["createTime"] = item["createTime"]
         
            # 将获取的数据交给pipelines，pipelines在settings.py中定义
            yield school
