python scrapy框架爬取haozu 数据

简介: 工作中需要数据,刚学习的python 还有 scarpy 如有大神指导,我必虚心学习。

1.创建项目
在控制台通过scrapy startproject 创建项目
我们通过scrapy startproject haozu 创建爬虫项目

haozu

2.创建爬虫文件
在控制台 进入spiders 文件夹下 通过scrapy genspider <网站域名>
scrapy genspider haozu_xzl www.haozu.com 创建爬虫文件

3.在爬虫文件中 haozu_xzl.py写代码 python version=3.6.0

-- coding: utf-8 --

import scrapy
import requests
from lxml import html
etree =html.etree
from ..items import HaozuItem
import random

class HaozuXzlSpider(scrapy.Spider):

# scrapy crawl haozu_xzl
name = 'haozu_xzl'
# allowed_domains = ['www.haozu.com/sz/zuxiezilou/']
start_urls = "http://www.haozu.com/sz/zuxiezilou/"
province_list = ['bj', 'sh', 'gz', 'sz', 'cd', 'cq', 'cs','dl','fz','hz','hf','nj','jian','jn','km','nb','sy',
                 'su','sjz','tj','wh','wx','xa','zz']
def start_requests(self):

    user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2'
    headers = {'User-Agent': user_agent}
    for s in self.province_list:
        start_url = "http://www.haozu.com/{}/zuxiezilou/".format(s)
        # 包含yield语句的函数是一个生成器,每次产生一个值,函数被冻结,被唤醒后再次产生一个值
        yield scrapy.Request(url=start_url, headers=headers, method='GET', callback=self.parse, \
                         meta={"headers": headers,"city":s})

def parse(self, response):
    lists = response.body.decode('utf-8')
    selector = etree.HTML(lists)
    elem_list = selector.xpath('/html/body/div[2]/div[2]/div/dl[1]/dd/div[2]/div[1]/a')
    print(elem_list,type(elem_list))
    for elem in elem_list[1:-1]:
        try:
            district = str(elem.xpath("text()"))[1:-1].replace("'",'')
            # district.remove(district[0])
            # district.pop()
            print(district,type(district))
            district_href =str(elem.xpath("@href"))[1:-1].replace("'",'')
            # district_href.remove(district_href[0])
            print(district_href,type(district_href))

            elem_url ="http://www.haozu.com{}".format(district_href)
            print(elem_url)
            yield scrapy.Request(url=elem_url, headers=response.meta["headers"], method='GET', callback=self.detail_url,
                                 meta={"district": district,"url":elem_url,"headers":response.meta["headers"],"city":response.meta["city"]})
        except Exception as e:
            print(e)
            pass
def detail_url(self, response):
    print("===================================================================")
    for i in range(1,50):
        # 组建url
        re_url = "{}o{}/".format(response.meta["url"],i)
        print(re_url)
        try:
            response_elem = requests.get(re_url,headers=response.meta["headers"])
            seles= etree.HTML(response_elem.content)
            sele_list = seles.xpath("/html/body/div[3]/div[1]/ul[1]/li")
            for sele in sele_list:
                href = str(sele.xpath("./div[2]/h1/a/@href"))[1:-1].replace("'",'')
                print(href)
                href_url = "http://www.haozu.com{}".format(href)
                print(href_url)
                yield scrapy.Request(url=href_url, headers=response.meta["headers"], method='GET',
                                     callback=self.final_url,
                                     meta={"district": response.meta["district"],"city":response.meta["city"]})
        except Exception as e:
            print(e)
            pass
def final_url(self,response):
    try:
        body = response.body.decode('utf-8')
        sele_body = etree.HTML(body)
        #获取价格 名称 地址
        item = HaozuItem()
        item["city"]= response.meta["city"]
        item['district']=response.meta["district"]
        item['addr'] = str(sele_body.xpath("/html/body/div[2]/div[2]/div/div/div[2]/span[1]/text()[2]"))[1:-1].replace("'",'')
        item['title'] = str(sele_body.xpath("/html/body/div[2]/div[2]/div/div/div[1]/h1/span/text()"))[1:-1].replace("'",'')
        price = str(sele_body.xpath("/html/body/div[2]/div[3]/div[2]/div[1]/span/text()"))[1:-1].replace("'",'')
        price_danwei=str(sele_body.xpath("/html/body/div[2]/div[3]/div[2]/div[1]/div/div/i/text()"))[1:-1].replace("'",'')
        print(price+price_danwei)
        item['price']=price+price_danwei
        yield item
    except Exception as e:
        print(e)
        pass  

4.修改items.py 文件

-- coding: utf-8 --

Define here the models for your scraped items

See documentation in:

https://doc.scrapy.org/en/latest/topics/items.html

import scrapy

class HaozuItem(scrapy.Item):

# define the fields for your item here like:
# name = scrapy.Field()
city = scrapy.Field()
district =scrapy.Field()
title = scrapy.Field()
addr =scrapy.Field()
price = scrapy.Field()

5修改settings.py

打开
ITEM_PIPELINES = {
'haozu.pipelines.HaozuPipeline': 300,
}

6 修改pipelines.py文件 这里可以自定义存储文件格式

-- coding: utf-8 --

Define your item pipelines here

Don't forget to add your pipeline to the ITEM_PIPELINES setting

See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

import csv

class HaozuPipeline(object):

def process_item(self, item, spider):
    f = open('./xiezilou2.csv', 'a+',encoding='utf-8',newline='')
    write = csv.writer(f)
    write.writerow((item['city'],item['district'],item['addr'],item['title'],item['price']))
    print(item)
    return item

7.启动框架

在控制台 输入 scrapy crawl haozu_xzl 启动程序

相关文章
|
3天前
|
XML 前端开发 数据格式
BeautifulSoup 是一个 Python 库,用于从 HTML 和 XML 文件中提取数据
BeautifulSoup 是 Python 的一个库,用于解析 HTML 和 XML 文件,即使在格式不规范的情况下也能有效工作。通过创建 BeautifulSoup 对象并使用方法如 find_all 和 get,可以方便地提取和查找文档中的信息。以下是一段示例代码,展示如何安装库、解析 HTML 数据以及打印段落、链接和特定类名的元素。BeautifulSoup 还支持更复杂的查询和文档修改功能。
11 1
|
1天前
|
设计模式 开发框架 数据库
Python Web开发主要常用的框架
Python Web开发框架包括Django、Flask、Tornado和Pyramid。Django适用于复杂应用,提供ORM、模板引擎等全套功能;Flask轻量级,易于扩展,适合小型至中型项目;Tornado擅长处理高并发,支持异步和WebSockets;Pyramid灵活强大,可适配多种数据库和模板引擎,适用于各种规模项目。选择框架需依据项目需求和技术栈。
8 2
|
4天前
|
数据采集 NoSQL 中间件
python-scrapy框架(四)settings.py文件的用法详解实例
python-scrapy框架(四)settings.py文件的用法详解实例
9 0
|
4天前
|
存储 数据采集 数据库
python-scrapy框架(三)Pipeline文件的用法讲解
python-scrapy框架(三)Pipeline文件的用法讲解
7 0
|
4天前
|
存储 数据采集 JSON
python-scrapy框架(二)items文件夹的用法讲解
python-scrapy框架(二)items文件夹的用法讲解
11 0
|
4天前
|
数据采集 前端开发 中间件
python-scrapy框架(一)Spider文件夹的用法讲解
python-scrapy框架(一)Spider文件夹的用法讲解
23 0
|
4天前
|
存储 JSON 数据挖掘
python序列化和结构化数据详解
python序列化和结构化数据详解
12 0
|
5天前
|
数据采集 数据可视化 数据挖掘
Python 与 PySpark数据分析实战指南:解锁数据洞见
Python 与 PySpark数据分析实战指南:解锁数据洞见
|
5天前
|
数据采集 数据处理 开发者
Python 中的数据处理技巧:高效数据操作的艺术
Python 在数据处理方面表现卓越,为开发者提供了丰富的工具和库以简化数据操作。在本文中,我们将探讨 Python 中数据处理的一些技巧,包括数据清洗、数据转换以及优化数据操作的最佳实践。通过掌握这些技巧,您可以在 Python 中更加高效地处理和分析数据。
|
6天前
|
机器学习/深度学习 自然语言处理 算法
Python遗传算法GA对长短期记忆LSTM深度学习模型超参数调优分析司机数据|附数据代码
Python遗传算法GA对长短期记忆LSTM深度学习模型超参数调优分析司机数据|附数据代码