中华英才网爬虫程序解析(1)-基础的爬虫程序实现

欢迎来到爬虫高级兼实战教程,打开你的IDE,开始python之旅吧!

中华英才网爬虫

在讲完python爬虫基础知识后,我们开始进行实战,在实战中我们会借实例来讲解爬虫的高级知识,爬虫程序已经公布于 https://github.com/code-nick-python/yingcaiwang-spider

在这个实例中,涉及到多线程threading和queue,分布式redis,接下来废话不多说,直接开始讲解!

爬虫程序基础编写

这里我们看到网页长这样:
在这里插入图片描述
我们的目标是爬取工作名称,工资,城市,学历,人数,公司和类别,接下来开始总结HTML代码吧!

首先看看连接数据库:

from pymongo import MongoClient
import pymysql

# a class for connect
class spider:
    # some things for mongodb and mysql
    def __init__(self, data=''):
        self.host = 'localhost'
        self.port = 27017
        self.user = 'root'
        self.passwd = 'nick2005'
        self.db = 'scraping'
        self.charset = 'utf8'
        self.data = data

    # connect to mongodb and remove all
    def connect_to_mongodb(self):
        # connect the mongodb and remove all
        client = MongoClient(host=self.host, port=self.port)
        db = client.blog_database
        collection = db.blog
        collection.remove({
   })
        return collection

    # connect the mysql and remove all
    def connect_to_mysql(self):
        conn = pymysql.connect(host=self.host, user=self.user, passwd=self.passwd, db=self.db, charset=self.charset)
        cur = conn.cursor()
        cur.execute('truncate table yingcaiwang;')
        return cur, conn

首先引入pymongo和pymysql分别是MongoDB和MySQL数据库的驱动

接下来在__init__中定义一些基础的类似于端口,密码之类的值。

然后定义一个连接mongodb数据库的函数connect_to_mongodb(),这里collection.remove代表移除全部数据,返回数据库。

接下来定义一个连

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值