python scrapy之爬取 zhengfu網站

#encoding=utf8
import scrapy
from govinfos.items import GovinfosItem


class GovInfos(scrapy.Spider):

    # 啓動爬蟲的名稱
    name = 'govinfo'
    # 爬蟲的範圍
    allowed_domains=['xzqh.mca.gov.cn']

    # 爬蟲的第一個url
    # start_urls = ['http://xzqh.mca.gov.cn/fuzzySearch']

    # 這裏是用post請求數據的
    def start_requests(self):
        url = 'http://xzqh.mca.gov.cn/fuzzySearch'

        # FormRequest 是Scrapy發送POST請求的方法
        yield scrapy.FormRequest(
            url = url,
            formdata = {"fs" : "%"},
            callback = self.parse
        )

    # 爬取結果分析
    def parse(self, response):
        print('%'*30)

        # print(response.body)
        node_list = response.xpath("//*[@class='info_table']/tr")
        for node in node_list:

            # 根據jiansuo_table 進行判斷是否包含,若不包含,則爲城市的第一個名稱
            # 第一個城市的第一個名稱
            td1 = node.xpath("./td[@class='name_left']/a/text()").extract()

            shen_address = node.xpath("./td/table[@class='jiansuo_table']/tr[@class='name_left']/td/a[@class='sheng_td']/text()").extract()
            shi_address = node.xpath("./td/table[@class='jiansuo_table']/tr[@class='name_left']/td/a[@class='shi_td']/text()").extract()
            qu_address = node.xpath("./td/table[@class='jiansuo_table']/tr[@class='name_left']/td[3]/a/text()").extract()
            print('%' * 30)
            print(td1)
            print(shen_address)
            print(shi_address)
            print(qu_address)

            # 駐地
            zhudi_address =node.xpath("./td[@class='name_left']/text()").extract()
            print(zhudi_address)
            # renkou
            person = node.xpath("./td[3]/text()").extract()
            print(person)
            #面積
            area = node.xpath("./td[4]/text()").extract()
            print(area)
            # 行政區劃
            xingzhen = node.xpath("./td[5]/text()").extract()
            print(xingzhen)
            # 區號
            quhao = node.xpath("./td[6]/text()").extract()
            print(quhao)
            # 郵編
            # print(node)
            youbian = node.xpath("./td[7]/text()").extract()
            print(youbian)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章