python爬取豆瓣短评_Python爬取豆瓣指定书籍的短评

Python爬取豆瓣指定书籍的短评

#!/usr/bin/python

# coding=utf-8

import re

import sys

import time

import random

import urllib

import urllib2

import MySQLdb

# 爬取豆瓣评论

class Douban:

# 构造函数

def __init__(self, url, name):

# 采集的地址

#self.url = 'https://book.douban.com/subject/26356948/comments/hot'

self.url = url

# 存储的文件名

self.filename = str(name) + '.txt'

# 数据库配置

self.database = {

'host': '127.0.0.1',

'username': 'root',

'password': 'root',

'database': 'douban',

'charset': 'utf8',

'table': str(name),

}

# HTTP请求超时

self.http_timeout = 10

# 请求计数器

self.request_counter = 0

# 错误代码计数器

self.error_counter = 0

# 数据库第一次错误

self.db_error_first = True

# 替换换行符

def replaceSpace(self, string):

string = string.replace('\r\n', ' ')

string = string.replace('\n', ' ')

string = string.replace('\r', ' ')

return string

# 清洗单页评论,返回为单条评论的列表

def getCommentsPage(self, url):

user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36'

headers = {'User-Agent': user_agent}

request = urllib2.Request(url, headers=headers)

try:

response = urllib2.urlopen(request, timeout=self.http_timeout)

comments_page_raw = response.read()

reg = r'.*?

(.*?).*?'

comments_page = re.findall(reg, comments_page_raw, re.I | re.M | re.S)

return comments_page

except urllib2.HTTPError, e:

errmsg = 'HTTP Error: ' + e.code + ',' + e.reason

print unicode(errmsg, 'utf-8')

if e.code == 403 or e.code == '403':

# 服务器拒绝服务,等等...等等... 等120秒

print unicode('服务器拒绝服务,等等...等等... 等120秒', 'utf-8')

time.sleep(120)

response = urllib2.urlopen(request, timeout=self.http_timeout)

comments_page_raw = response.read()

reg = r'.*?

(.*?).*?'

comments_page = re.findall(reg, comments_page_raw, re.I | re.M | re.S)

return comments_page

elif e.code == 404 or e.code == '404':

# not found... 已爬取完毕,结束程序

print unicode('not found... 已爬取完毕,结束程序', 'utf-8')

# 结束程序

sys.exit(0)

else:

print unicode('HTTP 未知错误', 'utf-8')

# 错误次数累加

self.error_counter += 1

# 判断是否进行中断

if self.error_counter == 20:

info = '错误次数已达%d次,结束程序' % self.error_counter

print unicode(info, 'utf-8')

sys.exit(1)

# 清洗单条评论,返回单条评论的昵称、头像、内容...的列表

def getCommentsItem(self, comment_raw):

# 昵称, 豆瓣主页,头像, 点赞, 星级评价, 日期, 评价内容

reg = r'

(.*?).*?.*? (.*?).*?

(.*?)

.*?
'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值