爬豆瓣读书Top250

摘要

本课题的主要目的是设计面向指定网站的网络爬虫程序,同时需要满足不同的性能要求。

搜索引擎作为一个辅助人们检索信息的工具。但是,这些通用性搜索引擎也存在着一定的局限性。不同领域、不同背景的用户往往具有不同的检索目的和需求,通过搜索引擎所返回的结果包含大量用户不关心的网页。为了解决这个问题,一个灵活的爬虫有着无可替代的重要意义。

Python爬取豆瓣读书Top250,可以自动分析构造的URL,实现对图书前250名信息的爬取。

关键词:python;爬虫;

Abstract

The main purpose of this topic is to design a web crawler program for a designated website, and it needs to meet different performance requirements.

The search engine serves as a tool to assist people in retrieving information. However, these universal search engines also have certain limitations. Users in different fields and different backgrounds often have different retrieval purposes and needs. The results returned by the search engine contain a large number of web pages that users do not care about. In order to solve this problem, a flexible crawler has irreplaceable importance.

Python crawls Douban Reading Top250, which can automatically analyze the constructed URL to achieve crawling of the top 250 information in the book.

Keywords: python,spider;

目录

1.引言 3

2. 系统结构 3

3. 实现代码 3

3.1得到指定一个URL的网页内容 4

模拟浏览器头部信息,向豆瓣服务器发送消息 4

3.2爬取网页 5

3.3.保存数据 7

把爬虫获取到的信息放入excel表格中 7

4. 实验 7

5. 总结和展望 8

 

1.引言

     互联网由庞大的数据信息组成,将数据有效的检索并组织呈现出来有着巨大的应用前景。搜索引擎作为一个辅助人们检索信息的工具成为用户访问万维网的入口和指南。但是,这些通用性搜索引擎也存在着一定的局限性。不同领域、不同背景的用户往往具有不同的检索目的和需求。比如你直接在百度上搜索最受欢迎的前250本书籍,可能你并不能找到符合自己要求的,因为通过搜索引擎所返回的结果包含大量用户不关心的网页。为了解决这个问题,一个灵活的爬虫有着无可替代的重要意义。

2.系统结构

网络爬虫(又称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本。另外一些不常使用的名字还有蚂蚁、自动索引、模拟程序或者蠕虫

Python爬取豆瓣前250的图书系统采用python爬虫技术,用urllib.request模块打开和读取指定的URL,urllib.error模块抛出异常;用re模块匹配正则表达式,获取我们想要爬取的网页内容;用bs4中的BeautifulSoup模块解析网页,获取数据;用xlwt模块把我们爬取到的数据写入excel表格中。

3.实现代码

 

import urllib.request, urllib.error  # 指定URL,获取网页数据
import re  # 正则表达式,进行文字匹配
import xlwt  # 进行excel操作
from bs4 import BeautifulSoup  # 网页解析,获取数据


def main():
    baseurl = "https://book.douban.com/top250?start="
    # 1.爬取网页
    datalist = getData(baseurl)
    savepath = ".\\豆瓣读书Top250.xls"
    # 3.保存数据
    saveData(datalist, savepath)


# 图书详情链接的规则
findLink = re.compile(r'<a class="nbg" href="(.*?)"')  # 创建正则表达式对象,表示规则(字符串模式)
# 图书图片
findImgSrc = re.compile(r'<img src="(.*?)"')
# 图书的中文名
findTitle = re.compile(r'<a.*title="(.*)">')
# 图书的外国名
findForeignName = re.compile(r'<span style="font-size:12px;">(.*)</span>')
# 图书评分
findRating = re.compile(r'<span class="rating_nums">(.*)</span>')
# 评价人数
findJudge = re.compile(r'<span class="pl">[\s\D]*(\d*)人评价.*?</span>', re.S)#\s匹配任意空白字符,等价于 [\t\n\r\f].,\D匹配任意非数字# re.S使 . 匹配包括换行在内的所有字符
# 概况
findInq = re.compile(r'<span class="inq">(.*)</span>')
# 图书的相关内容
findBd = re.compile(r'<p class="pl">(.*?)</p>')

3.1得到指定一个URL的网页内容

 

模拟浏览器头部信息,向豆瓣服务器发送消息

def askURL(url):
        head = {
        "User-Agent": "Mozilla / 5.0(Windows NT 10.0; WOW64) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 73.0.3683.86 Safari / 537.36"
    }
    # 用户代理,表示告诉豆瓣服务器,我们是什么类型的机器,浏览器(本质上是告诉浏览器,我们可以接受什么水平的)
    request = urllib.request.Request(url, headers=head)
    html = ""
    try:
        response = urllib.request.urlopen(request)
        html = response.read().decode("utf-8")
        # print(html)
    except urllib.error.URLError as e:
        if hasattr(e, "code"):  # hasattr(object, name)判断一个对象里面是否有name属性或者name方法
            print(e.code)
        if hasattr(e, "reason"):
            print(e.reason)
    return html

 

3.2爬取网页

首先我们访问“https://book.douban.com/top250”豆瓣图书链接,可以知道每一页有25条记录,第一页的链接“https://book.douban.com/top250?start=0”开始,后面每点击下一页start的值就+25,最后一页的链接为“https://book.douban.com/top250?start=225”。因此我们可以用BeautifulSoup得到每一页图书的信息,再用正则表达式re匹配到我们需要的内容。

 


def getData(baseurl):
    datalist = []
    for i in range(0, 10):  # 调用获取页面信息的函数,10次
        url = baseurl + str(i * 25)
        html = askURL(url)  # 保存获取到的网页源码

        # .逐一解析数据
        soup = BeautifulSoup(html, "html.parser")
        for item in soup.find_all('table'):
            #print(item)
            data = []  # 保留一部电影的所有信息
            item = str(item)

            # 图书详情的链接
            link = re.findall(findLink, item)[0]  # re库用来通过正则表达式查找指定的字符串
            data.append(link)  # 添加链接

            imgSrc = re.findall(findImgSrc, item)[0]
            data.append(imgSrc)  # 添加图片

            titles = re.findall(findTitle, item)[0]
            data.append(titles)

            foreignName = re.findall(findForeignName, item)
            if (len(foreignName) == 1):
                name = re.sub(':', "", foreignName[0]).strip()
                data.append(name)# 添加外国名
            else:
                data.append(' ')  # 外国名留空

            rating = re.findall(findRating, item)[0]
            data.append(rating)  # 评分

            judgeNum = re.findall(findJudge, item)[0]
            #print("人数:%s结束" % judgeNum)
            data.append(judgeNum)  # 添加评价人数


            inq = re.findall(findInq, item)
            if len(inq) != 0:
                inq = inq[0].replace("。 ", "")  # 去掉句号
                data.append(inq)  # 添加概述
            else:
                data.append(" ")  # 留空

            bd = re.findall(findBd, item)[0]
            # bd = re.sub('<br(\s+)?/>(\s+)?', " ", bd)  # 去掉<br/>
            bd = re.sub('/', " ", bd)  # 替换/
            bd = " ".join(bd.split())#默认为所有的空字符,包括空格、换行(\n)、制表符(\t)
            data.append(bd.strip())  # 去掉前后的空格strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)

            datalist.append(data)  # 把处理好的一部电影信息存到datalist
    #print(datalist)
    return datalist

3.3.保存数据

把爬虫获取到的信息放入excel表格中


def saveData(datalist, savepath):
    book = xlwt.Workbook(encoding="utf-8", style_compression=0)  # style_compression:表示是否压缩
    sheet = book.add_sheet('豆瓣图书Top250', cell_overwrite_ok=True)  # 创建workbook对象,cell_overwrite_ok每次覆盖
    col = ('图书详情链接', "图书链接", "图书中文名", "图书外国名", "评分", "评价数", "概况", "相关信息")
    for i in range(0, 8):
        sheet.write(0, i, col[i])  # 列名
    for i in range(0, 250):
        print("第%d条" % (i + 1))
        data = datalist[i]
        for j in range(0, 8):
            sheet.write(i + 1, j, data[j])
    book.save(savepath)


if __name__ == "__main__":  # 当程序执行时
    # 调用函数
    main()
    print("爬取完毕")

 

4.实验

实验结果:爬取到的图书Top250存到excel表格

 

5.总结和展望

通过本次课程,学会了python爬虫爬取静态的html页面,但是我还得多加学习,继续探索爬虫之路。当python的html页面由js渲染时,该如何爬呢?值得我接下来继续努力学习。

参考文献:

[1] (美)米切尔(Mitchell,R.)著;陶俊杰,陈小莉译. Python网络数据采集[M]. 北京:人民邮电出版社,2016.

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
好的,以下是Python爬虫豆瓣读书TOP250的代码: ``` import requests from bs4 import BeautifulSoup def get_html(url): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} try: r = requests.get(url, headers=headers) r.raise_for_status() r.encoding = r.apparent_encoding return r.text except: return "" def parse_html(html): soup = BeautifulSoup(html, "html.parser") book_list = [] for book in soup.find_all('tr', class_='item'): book_name = book.find('div', class_='pl2').a['title'] book_author = book.find('p', class_='pl').get_text() book_rating = book.find('span', class_='rating_nums').get_text() book_pl = book.find('span', class_='pl').get_text() book_list.append([book_name, book_author, book_rating, book_pl]) return book_list def print_book_list(book_list): tplt = "{0:^10}\t{1:{4}^20}\t{2:^10}\t{3:^10}" print(tplt.format("序号", "书籍名称", "评分", "评价人数", chr(12288))) for i, book in enumerate(book_list): print(tplt.format(i + 1, book[0], book[2], book[3], chr(12288))) def main(): start_url = 'https://book.douban.com/top250?start=' book_list = [] for i in range(0, 250, 25): url = start_url + str(i) html = get_html(url) book_list.extend(parse_html(html)) print_book_list(book_list) if __name__ == '__main__': main() ``` 这个代码会豆瓣读书TOP250的书籍名称、作者、评分和评价人数,并将结果按照格式输出。你可以直接运行这个代码,或者根据自己的需求对其进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

少壮Strive

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值