豆瓣电影 TOP250 和书籍 TOP250 爬虫

最近开始玩 Python , 学习爬虫相关知识的时候,心血来潮,爬取了豆瓣电影TOP250 和书籍TOP250, 这里记录一下自己玩的过程。

电影 TOP250 爬虫
  1. import requests
  2. from bs4 import BeautifulSoup
  3. import time
  4. def getlist(list_url):
  5. time.sleep(2)
  6. res = requests.get(list_url)
  7. soup = BeautifulSoup(res.text, 'html.parser')
  8. movie_list = soup.select('.grid_view li')
  9. for m in movie_list:
  10. rank = m.select('em')[0].text
  11. score = m.select('.rating_num')[0].text
  12. title = m.select('.title')[0].text
  13. direct = m.select('.info .bd p')[0].text.strip()
  14. actor = '\n主演:'.join(direct.split(' 主演:'))
  15. director = '年代:'.join(actor.split(' '))
  16. if m.select('.inq'):
  17. comments = m.select('.inq')[0].text.strip()
  18. else:
  19. comments = 'None'
  20. movie.append(
  21. '排名: ' + rank + '\n'
  22. + '评分: ' + score + '\n'
  23. + '片名: ' + title + '\n'
  24. + director + '\n'
  25. + '评论: ' + comments + '\n'
  26. + '\n')
  27. if soup.select('.next a'):
  28. asoup = soup.select('.next a')[0]['href']
  29. next_page = seed_url + asoup
  30. getlist(next_page)
  31. else:
  32. print('结束')
  33. return movie
  34. def write(movies):
  35. with open('movie.txt', 'w', encoding='utf8') as m:
  36. for a in movies:
  37. m.write(a)
  38. def main():
  39. write(getlist(seed_url))
  40. pass
  41. if __name__ == '__main__':
  42. seed_url = 'https://movie.douban.com/top250'
  43. movie = []
  44. main()
书籍 TOP250 爬虫
  1. import bs4
  2. import requests
  3. import re
  4. from bs4 import BeautifulSoup
  5. from operator import itemgetter
  6. def getHtmlText(url):
  7. try:
  8. r = requests.get(url)
  9. r.raise_for_status()
  10. r.encoding = r.apparent_encoding
  11. return r.text
  12. except:
  13. return ""
  14. def parserText(text, book_list):
  15. soup = BeautifulSoup(text, 'html.parser')
  16. for table in soup('table', {'width': '100%'}):
  17. if isinstance(table, bs4.element.Tag):
  18. tds = table.find('tr')('td')
  19. divs = tds[1]('div')
  20. content = {}
  21. for div in divs:
  22. if isinstance(div, bs4.element.Tag):
  23. if div.find('a'):
  24. name = div.find('a').attrs['title']
  25. content.update({"书名": name})
  26. if div.select('.rating_nums'):
  27. score = div.select('.rating_nums')[0].text
  28. content.update({"评分": score})
  29. if div.select('.pl'):
  30. people_num = div.select('.pl')[0].text
  31. regex = re.compile(r'[\d]{1,10}')
  32. content.update({"评价人数": regex.findall(people_num)[0]})
  33. ps = tds[1]('p')
  34. for p in ps:
  35. if isinstance(p, bs4.element.Tag):
  36. if p.attrs['class'][0] == 'quote':
  37. description = p.find('span').string
  38. content.update({"介绍": description})
  39. if p.attrs['class'][0] == 'pl':
  40. author = p.string
  41. content.update({"作者信息": author})
  42. book_list.append(content)
  43. next_books = soup.find('span', {'class': 'next'})
  44. if next_books.find('a'):
  45. a = next_books.find('a').attrs['href']
  46. text = getHtmlText(a)
  47. parserText(text, books)
  48. return book_list
  49. def sortedBookTop250(book_list):
  50. tmp = sorted(book_list, key=itemgetter('评分'), reverse=True)
  51. for i in range(len(tmp)):
  52. tmp[i].update({"排名": i + 1})
  53. return tmp
  54. def writeToFile(book_list):
  55. with open('good_books.txt', 'w', encoding='utf8') as book_file:
  56. for book in book_list:
  57. for key, value in book.items():
  58. book_file.write(f'{key}:{value}\n')
  59. book_file.write('\n')
  60. pass
  61. def main():
  62. text = getHtmlText(seed_url)
  63. book_list = parserText(text, books)
  64. writeToFile(sortedBookTop250(book_list))
  65. pass
  66. if __name__ == '__main__':
  67. seed_url = "https://book.douban.com/top250"
  68. books = []
  69. main()

总结

以上直接贴出了代码,这是很简单的两段代码,主要用到了 requests 库和 beautifulsoup 库,需要的可以直接拿去,或者直接去我的 GIthub上拿 movies.txtgood_books.txt

豆瓣电影TOP250和书籍TOP250爬虫的更多相关文章

  1. 练习:一只豆瓣电影TOP250的爬虫

    练习:一只豆瓣电影TOP250爬虫 练习:一只豆瓣电影TOP250爬虫 ①创建project ②编辑items.py import scrapyclass DoubanmovieItem(scrapy ...

  2. python3 爬虫---爬取豆瓣电影TOP250

    第一次爬取的网站就是豆瓣电影 Top 250,网址是:https://movie.douban.com/top250?start=0&filter= 分析网址'?'符号后的参数,第一个参数's ...

  3. scrapy爬虫框架教程(二)-- 爬取豆瓣电影TOP250

    scrapy爬虫框架教程(二)-- 爬取豆瓣电影TOP250 前言 经过上一篇教程我们已经大致了解了Scrapy的基本情况,并写了一个简单的小demo.这次我会以爬取豆瓣电影TOP250为例进一步为大 ...

  4. 一起学爬虫——通过爬取豆瓣电影top250学习requests库的使用

    学习一门技术最快的方式是做项目,在做项目的过程中对相关的技术查漏补缺. 本文通过爬取豆瓣top250电影学习python requests的使用. 1.准备工作 在pycharm中安装request库 ...

  5. Scrapy爬虫(4)爬取豆瓣电影Top250图片

      在用Python的urllib和BeautifulSoup写过了很多爬虫之后,本人决定尝试著名的Python爬虫框架--Scrapy.   本次分享将详细讲述如何利用Scrapy来下载豆瓣电影To ...

  6. 零基础爬虫----python爬取豆瓣电影top250的信息(转)

    今天利用xpath写了一个小爬虫,比较适合一些爬虫新手来学习.话不多说,开始今天的正题,我会利用一个案例来介绍下xpath如何对网页进行解析的,以及如何对信息进行提取的. python环境:pytho ...

  7. python爬虫 Scrapy2-- 爬取豆瓣电影TOP250

    sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...

  8. Python爬虫----抓取豆瓣电影Top250

    有了上次利用python爬虫抓取糗事百科的经验,这次自己动手写了个爬虫抓取豆瓣电影Top250的简要信息. 1.观察url 首先观察一下网址的结构 http://movie.douban.com/to ...

  9. Python爬虫入门:爬取豆瓣电影TOP250

    一个很简单的爬虫. 从这里学习的,解释的挺好的:https://xlzd.me/2015/12/16/python-crawler-03 分享写这个代码用到了的学习的链接: BeautifulSoup ...

随机推荐

  1. Python模块之snmp-cmds,easysnmp

    一.简介 snmp-cmds模块通过SNMP与目标设备进行通信,此模块适用于windows,此模块是基于系统已安装了net-snmp环境easysnmp模块通过SNMP与谬表设备进行通信,此模块用于l ...

  2. Springboot源码分析之EnableAspectJAutoProxy

    摘要: Spring Framwork的两大核心技术就是IOC和AOP,AOP在Spring的产品线中有着大量的应用.如果说反射是你通向高级的基础,那么代理就是你站稳高级的底气.AOP的本质也就是大家 ...

  3. SpringBoot进阶教程(六十一)intellij idea project下建多个module搭建架构(下)

    在上一篇文章<SpringBoot进阶教程(六十)intellij idea project下建多个module(上)>中,我们已经介绍了在intellij idea中创建project之 ...

  4. N*N矩阵的旋转 不开辟新空间

    /* N*N矩阵的旋转 不开辟新空间 programmer:qpz time:2014-11-09 */ #include <iostream> using namespace std; ...

  5. odoo通过actions.client进行自定义页面

    一.使用原因 由于odoo自带页面在项目开发过程中无法满足使用,需要使用到动作ir.actions.client进行自定义视图的开发,实现自定义的xml视图开发. 二.实现目标 三.开发过程 1.项目 ...

  6. jquery实现表格导入到Excel(加图片)

      话不多说直接上代码 第一步:导入jquery的插件https://github.com/rainabba/jquery-table2excel HTML部分: 第二步:添加一个按钮 <but ...

  7. Hugo

    快速开始 安装Hugo 1.二进制安装(推荐:简单.快速) 到 Hugo Releases 下载对应的操作系统版本的Hugo二进制文件(hugo或者hugo.exe) Mac下直接使用 ==Homeb ...

  8. Badboy - 从excel中读取数据

    参考: http://leafwf.blog.51cto.com/872759/1119161 http://www.51testing.com/html/00/130600-1367743.html ...

  9. SDU暑期集训排位(5)

    SDU暑期集训排位(5) A. You're in the Army Now 题意 类似选志愿.每个人有 mark,有优先级从高到低的志愿. 做法 定睛一看,鲨鼻题.然后 WA. 为什么会 WA 呢? ...

  10. POJ-1213 How Many Tables( 并查集 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday. ...