【爬虫入门】【正则表达式】【同步】爬取人人车车辆信息1.0

编程知识 更新时间:2023-05-03 01:18:09
# 爬取人人车车车辆信息。

from urllib.request import urlopen
from urllib.error import HTTPError


import re, sqlite3

class RRCSpider(object):
    """
    人人车爬虫类
    """
    def __init__(self):
        pass
        
    def get_list_html(self, page_num):
        """
        获取列表页源代码
        :param page_num: 列表页的页码
        :return: 返回网页源代码
        """
        list_url = 'https://www.renrenche/zz/ershouche/p{}/'.format(page_num)
        try:
            list_html = urlopen(list_url).read().decode()
        except HTTPError as e:
            print('列表页异常:url={}, error={}'.format(list_url, e))
            return None, None
        else:
            return list_html, list_url

    def parse_list_html(self, list_html, list_url):
        """
        解析列表页数据
        :param list_html: 列表页网页源代码
        :return: 返回每一个数据的详情页地址
        """
        # 利用正则表达式提取列表页中所有二手车的详情页的链接。
        detail_urls = re.findall(repile(r'<li class="span6 list-item.*?".*?<a.*?href="(.*?)".*?class="thumbnail"', re.S), list_html)
        if detail_urls:
            return detail_urls
        else:
            print('列表页数据为空:url={}'.format(list_url))
            return None

    def get_detail_html(self, detail_url):
        """
        获取详情页源代码
        :param detail_url: 详情页的url
        :return: 返回详情页网页源代码
        """
        try:
            detail_html = urlopen(detail_url).read().decode()
        except HTTPError as e:
            print('详情页异常:url={}, error={}'.format(detail_url, e))
            return None, None
        else:
            return detail_html, detail_url

    def parse_detail_html(self, detail_html, detail_url):
        """
        解析详情页数据
        :param detail_html: 详情页网页源代码
        :return: None
        """
        # [('本天', '6.7', '2010')]
        data = re.findall(repile(r'<h1 class="title-name rrc.*?">(.*?)</h1>.*?<p class="price.*?">(.*?)</p>.*?<p class="money.*?首付(.*?)<.*?月供(.*?)</p>.*?<ul class=".*?box-list-primary-detail">.*?<strong class="car-summary rrc.*?">(.*?)</strong>.*?<p class="small-title rrc.*?">(.*?)</p>.*?<strong.*?id="car-licensed">(.*?)</strong>.*?<p>.*?<strong class="car-summary">(.*?)</strong>.*?<p class="transfer-record">.*?<strong.*?>(.*?)</strong>', re.S), detail_html)[0]
        print(data)

    def start_spider(self, num):
        """
        爬虫程序启动入口
        :return:
        """
        print('正在请求第{}页'.format(num))
        list_html, list_url = self.get_list_html(num)
        if list_html:
            detail_urls = self.parse_list_html(list_html, list_url)
            if detail_urls:
                for detail_url in detail_urls:
                    url = 'https://www.renrenche' + detail_url
                    detail_html, d_url = self.get_detail_html(url)
                    if detail_html:
                        self.parse_detail_html(detail_html, d_url)

if __name__ == '__main__':
    obj = RRCSpider()

    # 这是同步for循环
    for x in range(1,2):
        obj.start_spider(x)

 

更多推荐

【爬虫入门】【正则表达式】【同步】爬取人人车车辆信息1.0

本文发布于:2023-04-29 20:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/d19bc717270676c1f1c2f36da30bef6c.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:爬虫   入门   车辆   信息   正则表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 112077文章数
  • 28529阅读数
  • 0评论数