Dotcpp  >  编程教程  >  Python实战项目  >  Python爬虫入门抓取豆瓣内容二

Python爬虫入门抓取豆瓣内容二

点击打开在线编译器,边学边练

1. 问题分析

        我们在上一节爬取了网页的全部信息,下面我们还要这样html代码中找到我们所需要的内容,因此我们要根据问题进入网站中,去解析网页中的信息。

 爬取豆瓣电影信息1

        从页面中可以发现,我们需要爬取的信息分别存在于不同的分区当中,那么我们来检查一下页面的元素,右键页面检查网页源代码或者F12。

 爬取豆瓣电影信息2

        在分析网页之前,我们先规定一下解析之后的存储方式,这里我们采用列表来存储所有的信息,然后列表中的每一项对应一个字典,每一个字典再对应多种信息。

movies = []#首先定义一个列表来存储所有信息

        通过分析我们可以确定title的位置是名为‘hd’的‘div’下的第一个‘a’中的第一个‘span’,因此我们可以通过下面代码来锁定每一个电影的名字,然后放到一个字典中。

moviename = each.find('div', class_='hd').a.span.text.strip()
movie['title'] = moviename#字典的一项

        相同的方式可以再根据定位找到导演名的源码,但是这个源码中包含了很多信息,所以我们要通过正则表达式进行过滤。

 

爬取豆瓣电影信息3

info = each.find('div', class_='bd').p.text.strip()

        首先找到了这个标签下的所有内容,我们再通过正则表达式过滤掉无关信息。

info = info.replace('\n', "")#过滤回车
info = info.replace(" ", "")#过滤空格
info = info.replace("\xa0", "")#过滤不间断空白符
director = re.findall(r'[导演:].+[主演:]', info)[0]
director = director[3:len(director) - 6]

        然后定义为字典的一项。

movie['director'] = director#字典的一项

        我们可以发现电影类型也在这个‘p’标签中,我们也直接通过正则表达式来获取该信息。

plot = re.findall(r'[0-9]*[/].+[/].+', info)[0]
plot = plot[1:]
plot = plot[plot.index('/') + 1:]
plot = plot[plot.index('/') + 1:]
movie['plot'] = plot#添加为字典的 一项

        最后再锁定评分信息。

star = each.find('div', class_='star')
star = star.find('span', class_='rating_num').text.strip()

        然后继续以字典的形式保存。

movie['star'] = star

        最后把这个字典添加到列表中并遍历输出。

movies.append(movie)#把字典加到列表中
for i in movies:#遍历输出
    print(i)

2. 完整代码

import re
import requests
from bs4 import BeautifulSoup
for i in range(1):
    headers = {#模拟浏览器进行访问
        'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 
        (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36',
        'Host': 'movie.douban.com'
    }
    res = 'https://movie.douban.com/top250?start='+str(25*i)#25次
    r = requests.get(res, headers=headers, timeout=10)#设置超时时间
    soup = BeautifulSoup(r.text, "html.parser")#设置解析方式,也可以使用其他方式。
    div_list = soup.find_all('div', class_='item')
    movies = []
    for each in div_list:
        movie = {}
        moviename = each.find('div', class_='hd').a.span.text.strip()
        movie['title'] = moviename
        rank = each.find('div', class_='pic').em.text.strip()
        movie['rank'] = rank
        info = each.find('div', class_='bd').p.text.strip()
        info = info.replace('\n', "")
        info = info.replace(" ", "")
        info = info.replace("\xa0", "")
        director = re.findall(r'[导演:].+[主演:]', info)[0]
        director = director[3:len(director) - 6]
        movie['director'] = director
        release_date = re.findall(r'[0-9]{4}', info)[0]
        movie['release_date'] = release_date
        plot = re.findall(r'[0-9]*[/].+[/].+', info)[0]
        plot = plot[1:]
        plot = plot[plot.index('/') + 1:]
        plot = plot[plot.index('/') + 1:]
        movie['plot'] = plot
        star = each.find('div', class_='star')
        star = star.find('span', class_='rating_num').text.strip()
        movie['star'] = star
        movies.append(movie)
    for i in movies:
        print(i)

3. 运行结果

        控制台:

 爬取豆瓣电影信息4

4. 总结

        在这个实例中,我们主要学习如何去网页的源码中找到相应的信息,BeautifulSoup可以帮助我们迅速定位,再结合正则表达式来完成信息的匹配,下一节我们把这些数据保存到数据库当中。



本文固定URL:https://www.dotcpp.com/course/329

Python教程
第一章 Python入门
第二章 Python基础语法
第三章 Python入门语法
第四章 Python核心语法
第五章 Python函数
第六章 Python面向对象编程
第七章 Python模块
第八章 Python异常处理和程序调试
第九章 Python文件及目录操作
第十章 PythonGUI编程
第十一章 Python进程和线程
第十二章 Python数据库管理
第十三章 Python算法
第十四章 Python爬虫
第十五章 Python实战项目
第十六章 后记
Dotcpp在线编译      (登录可减少运行等待时间)