欢迎来到爬虫高级兼实战教程,打开你的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代表移除全部数据,返回数据库。
接下来定义一个连