认识的一些朋友也养了猫,比如橘猫、英短、加菲猫之类的,看他们发朋友圈撸猫,老羡慕了,猫咪真的太可爱啦。发现一个专门交易猫猫的网站—猫猫交易网可以云看猫:http://www.maomijiaoyi.com/
- import requests
- import re
- import csv
- from lxml import etree
- from tqdm import tqdm
- from fake_useragent import UserAgent
- # 随机产生请求头
- ua = UserAgent(verify_ssl=False, path='fake_useragent.json')
- def random_ua(): # 用于随机切换请求头
- headers = {
- "Accept-Encoding": "gzip",
- "Accept-Language": "zh-CN",
- "Connection": "keep-alive",
- "Host": "www.maomijiaoyi.com",
- "User-Agent": ua.random
- }
- return headers
- def create_csv(): # 创建保存数据的csv
- with open('./data/cat_kind.csv', 'w', newline='', encoding='utf-8') as f:
- wr = csv.writer(f)
- wr.writerow(['品种', '参考价格', '中文学名', '别名', '祖先', '分布区域',
- '原产地', '体型', '原始用途', '今日用途', '分组', '身高',
- '体重', '寿命', '整体', '毛发', '颜色', '头部', '眼睛',
- '耳朵', '鼻子', '尾巴', '胸部', '颈部', '前驱', '后驱',
- '基本信息', 'FCI标准', '性格特点', '生活习性', '优点/缺点',
- '喂养方法', '鉴别挑选'])
- def scrape_page(url1): # 获取HTML网页源代码 返回文本
- response = requests.get(url1, headers=random_ua())
- # print(response.status_code)
- response.encoding = 'utf-8'
- return response.text
- def get_cat_urls(html1): # 获取每个品种猫咪详情页url
- dom = etree.HTML(html1)
- lis = dom.xpath('//div[@class="pinzhong_left"]/a')
- cat_urls = []
- for li in lis:
- cat_url = li.xpath('./@href')[0]
- cat_url = 'http://www.maomijiaoyi.com' + cat_url
- cat_urls.append(cat_url)
- return cat_urls
- def get_info(html2): # 爬取每个品种猫咪详情页里的有关信息
- # 品种
- kind = re.findall('div class="line1">.*?<div class="name">(.*?)<span>', html2, re.S)[0]
- kind = kind.replace('\r','').replace('\n','').replace('\t','')
- # 参考价格
- price = re.findall('<div>参考价格:</div>.*?<div>(.*?)</div>', html2, re.S)[0]
- price = price.replace('\r', '').replace('\n', '').replace('\t', '')
- # 中文学名
- chinese_name = re.findall('<div>中文学名:</div>.*?<div>(.*?)</div>', html2, re.S)[0]
- chinese_name = chinese_name.replace('\r', '').replace('\n', '').replace('\t', '')
- # 别名
- other_name = re.findall('<div>别名:</div>.*?<div>(.*?)</div>', html2, re.S)[0]
- other_name = other_name.replace('\r', '').replace('\n', '').replace('\t', '')
- # 祖先
- ancestor = re.findall('<div>祖先:</div>.*?<div>(.*?)</div>', html2, re.S)[0]
- ancestor = ancestor.replace('\r', '').replace('\n', '').replace('\t', '')
- # 分布区域
- area = re.findall('<div>分布区域:</div>.*?<div>(.*?)</div>', html2, re.S)[0]
- area = area.replace('\r', '').replace('\n', '').replace('\t', '')
- # 原产地
- source_area = re.findall('<div>原产地:</div>.*?<div>(.*?)</div>', html2, re.S)[0]
- source_area = source_area.replace('\r', '').replace('\n', '').replace('\t', '')
- # 体型
- body_size = re.findall('<div>体型:</div>.*?<div>(.*?)</div>', html2, re.S)[0]
- body_size = body_size.replace('\r', '').replace('\n', '').replace('\t', '').strip()
- # 原始用途
- source_use = re.findall('<div>原始用途:</div>.*?<div>(.*?)</div>', html2, re.S)[0]
- source_use = source_use.replace('\r', '').replace('\n', '').replace('\t', '')
- # 今日用途
- today_use = re.findall('<div>今日用途:</div>.*?<div>(.*?)</div>', html2, re.S)[0]
- today_use = today_use.replace('\r', '').replace('\n', '').replace('\t', '')
- # 分组
- group = re.findall('<div>分组:</div>.*?<div>(.*?)</div>', html2, re.S)[0]
- group = group.replace('\r', '').replace('\n', '').replace('\t', '')
- # 身高
- height = re.findall('<div>身高:</div>.*?<div>(.*?)</div>', html2, re.S)[0]
- height = height.replace('\r', '').replace('\n', '').replace('\t', '')
- # 体重
- weight = re.findall('<div>体重:</div>.*?<div>(.*?)</div>', html2, re.S)[0]
- weight = weight.replace('\r', '').replace('\n', '').replace('\t', '')
- # 寿命
- lifetime = re.findall('<div>寿命:</div>.*?<div>(.*?)</div>', html2, re.S)[0]
- lifetime = lifetime.replace('\r', '').replace('\n', '').replace('\t', '')
- # 整体
- entirety = re.findall('<div>整体</div>.*?<!-- 页面小折角 -->.*?<div></div>.*?<div>(.*?)</div>', html2, re.S)[0]
- entirety = entirety.replace('\r', '').replace('\n', '').replace('\t', '').strip()
- # 毛发
- hair = re.findall('<div>毛发</div>.*?<div></div>.*?<div>(.*?)</div>', html2, re.S)[0]
- hair = hair.replace('\r', '').replace('\n', '').replace('\t', '').strip()
- # 颜色
- color = re.findall('<div>颜色</div>.*?<div></div>.*?<div>(.*?)</div>', html2, re.S)[0]
- color = color.replace('\r', '').replace('\n', '').replace('\t', '').strip()
- # 头部
- head = re.findall('<div>头部</div>.*?<div></div>.*?<div>(.*?)</div>', html2, re.S)[0]
- head = head.replace('\r', '').replace('\n', '').replace('\t', '').strip()
- # 眼睛
- eye = re.findall('<div>眼睛</div>.*?<div></div>.*?<div>(.*?)</div>', html2, re.S)[0]
- eye = eye.replace('\r', '').replace('\n', '').replace('\t', '').strip()
- # 耳朵
- ear = re.findall('<div>耳朵</div>.*?<div></div>.*?<div>(.*?)</div>', html2, re.S)[0]
- ear = ear.replace('\r', '').replace('\n', '').replace('\t', '').strip()
- # 鼻子
- nose = re.findall('<div>鼻子</div>.*?<div></div>.*?<div>(.*?)</div>', html2, re.S)[0]
- nose = nose.replace('\r', '').replace('\n', '').replace('\t', '').strip()
- # 尾巴
- tail = re.findall('<div>尾巴</div>.*?<div></div>.*?<div>(.*?)</div>', html2, re.S)[0]
- tail = tail.replace('\r', '').replace('\n', '').replace('\t', '').strip()
- # 胸部
- chest = re.findall('<div>胸部</div>.*?<div></div>.*?<div>(.*?)</div>', html2, re.S)[0]
- chest = chest.replace('\r', '').replace('\n', '').replace('\t', '').strip()
- # 颈部
- neck = re.findall('<div>颈部</div>.*?<div></div>.*?<div>(.*?)</div>', html2, re.S)[0]
- neck = neck.replace('\r', '').replace('\n', '').replace('\t', '').strip()
- # 前驱
- font_foot = re.findall('<div>前驱</div>.*?<div></div>.*?<div>(.*?)</div>', html2, re.S)[0]
- font_foot = font_foot.replace('\r', '').replace('\n', '').replace('\t', '').strip()
- # 后驱
- rear_foot = re.findall('<div>前驱</div>.*?<div></div>.*?<div>(.*?)</div>', html2, re.S)[0]
- rear_foot = rear_foot.replace('\r', '').replace('\n', '').replace('\t', '').strip()
- # 保存前面猫猫的各种有关信息
- cat = [kind, price, chinese_name, other_name, ancestor, area, source_area,
- body_size, source_use, today_use, group, height, weight, lifetime,
- entirety, hair, color, head, eye, ear, nose, tail, chest, neck, font_foot, rear_foot]
- # 提取标签栏信息(基本信息-FCI标准-性格特点-生活习性-优缺点-喂养方法-鉴别挑选)
- html2 = etree.HTML(html2)
- labs = html2.xpath('//div[@class="property_list"]/div')
- for lab in labs:
- text1 = lab.xpath('string(.)')
- text1 = text1.replace('\n','').replace('\t','').replace('\r','').replace(' ','')
- cat.append(text1)
- return cat
- def write_to_csv(data): # 保存数据 追加写入
- with open('./data/cat_kind.csv', 'a+', newline='', encoding='utf-8') as fn:
- wr = csv.writer(fn)
- wr.writerow(data)
- if __name__ == '__main__':
- # 创建保存数据的csv
- create_csv()
- # 猫咪品种页面url
- base_url = 'http://www.maomijiaoyi.com/index.php?/pinzhongdaquan_5.html'
- # 获取品种页面中的所有url
- html = scrape_page(base_url)
- urls = get_cat_urls(html)
- # 进度条可视化运行情况 就不打印东西来看了
- pbar = tqdm(urls)
- # 开始爬取
- for url in pbar:
- text = scrape_page(url)
- info = get_info(text)
- write_to_csv(info)
由于数据量较大,可以分开爬取,先获取到每一页中的所有猫猫详情交易链接的 url 保存到csv,再读取 csv 中的 url 来请求,爬取每条交易数据,爬虫思路跟前面类似,为了加快爬取效率,可以使用多线程或者异步爬虫。最终获取了 20W+ 条数据。
橘猫是世界各地都有的,不愧是我大橘猫。俗话说 "十个橘猫九个胖还有一个压塌炕"。橘猫比起其他花色的猫咪更喜欢吃东西,它们的食欲很好,能更好地生存,可能这也是橘猫在世界范围都有的原因吧。可它却是小型猫,橘猫小时候颜值一般挺高,看起来小小的一只,又嫩又可爱的,但等橘猫长大以后,才真正地意识到什么是 "橘足轻重"。
价格最贵的发现均为 3000 元的布偶猫。查阅资料发现,布偶猫,大型猫咪,不仅购买的时候价格高昂,饲养成本也比较高,因为食量和运动量都比较大,而且美容等相关费用也会高一些。