前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >利用bs4和requests爬取豆瓣Top250排行版电影信息

利用bs4和requests爬取豆瓣Top250排行版电影信息

作者头像
梦溪
发布2021-08-09 15:06:42
1.1K0
发布2021-08-09 15:06:42
举报
文章被收录于专栏:梦溪博客梦溪博客

利用bs4和requests爬取豆瓣Top250排行版电影信息

豆瓣Top250

1.工具

Python requests bs4 csv

2.思路

a.导入第三方库
代码语言:javascript
复制
import requests
import bs4
import unicodecsv as csv
b.使用requests获取网页源码

image.png

代码语言:javascript
复制
header = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/90.0.4430.93 Safari/537.36 "
}
#拼接链接
for i in range(0, 10):
    html = requests.get('https://movie.douban.com/top250?start=' + str(start), headers=header)
    # 不加.text 或 .content 就会出现response 200
    print(html.text)
    html.encoding = 'utf-8'
    start += 25
c.使用bs4提取有效信息
代码语言:javascript
复制
soup = bs4.BeautifulSoup(html.text, 'html.parser')

    for item in soup.find_all('div', 'info'):
        title = item.div.span.string
        # print(title)
        yearline = item.find('div', 'bd').p.contents[2].string
        yearline = yearline.replace('\n', '')
        yearline = yearline.replace(' ', '')
        year = yearline[0:4]
        rating = item.find('span', {'class': 'rating_num'}).get_text()
        oneresult = [title, rating, year]
        result.append(oneresult)
    print(result)
d.存储文件
代码语言:javascript
复制
    with open('top_250.csv', 'wb') as f:
        w = csv.writer(f)
        w.writerows(result)
        f.close()

完整代码

代码语言:javascript
复制
#导入requests bs4
import requests
import bs4
import unicodecsv as csv
start = 0
result = []

header = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/90.0.4430.93 Safari/537.36 "
}
#拼接链接
for i in range(0, 10):
    html = requests.get('https://movie.douban.com/top250?start=' + str(start), headers=header)
    # 不加.text 或 .content 就会出现response 200
    print(html.text)
    html.encoding = 'utf-8'
    start += 25
    soup = bs4.BeautifulSoup(html.text, 'html.parser')

    for item in soup.find_all('div', 'info'):
        title = item.div.span.string
        # print(title)
        yearline = item.find('div', 'bd').p.contents[2].string
        yearline = yearline.replace('\n', '')
        yearline = yearline.replace(' ', '')
        year = yearline[0:4]
        rating = item.find('span', {'class': 'rating_num'}).get_text()
        oneresult = [title, rating, year]
        result.append(oneresult)
    print(result)
  

    with open('top_250.csv', 'wb') as f:
        w = csv.writer(f)
        w.writerows(result)
        f.close()

付费内容


版权属于:Cyril

本文链接:https://cloud.tencent.com/developer/article/1858304

转载时须注明出处及本声明

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021 年 06 月,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 利用bs4和requests爬取豆瓣Top250排行版电影信息
    • 1.工具
      • 2.思路
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档