鱼C论坛

 找回密码
 立即注册
查看: 1048|回复: 1

python爬虫爬取豆瓣top250

[复制链接]
发表于 2018-2-12 12:28:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import requests
from bs4 import BeautifulSoup


def getHTML(url):
    try:
        r = requests.get(url)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except:
        return ''

def print_info(lis):
    count = 0
    for movie in lis:
        count += 1
        flist = 'Top{}\n电影名:{}\n评价人数:{}\n分数:{}\n链接:{}\n一句话影评:{}\n'
        print(flist.format(count, movie[0], movie[1], movie[2], movie[3], movie[4]))

def parasepage(html, lis):
    soup = BeautifulSoup(html, 'html.parser')
    movies = soup.find_all('div', class_ = 'info')
    for info in movies:
        name_info = info.find('span', class_ = 'title').string

        comment = info.find('span', class_ = "rating_num")
        score_info = comment.string
        people_info = comment.find_next_sibling().find_next_sibling().string
        link_info = info.find('a').get('href')
        try:
            review_info = info.find('span', class_ = 'inq').string
        except:
            review_info = '暂无信息'
        lis.append([name_info, people_info, score_info, link_info, review_info])
    print_info(lis)


def main():
    depth = 11
    lis = []
    start_url = 'https://movie.douban.com/top250?start='
    for i in range(depth):
        url = start_url + str(i * 25) + '&filter='
        html = getHTML(url)
        parasepage(html, lis)


if __name__ == '__main__':
    main()

代码在sublime上运行爬到top210报错'gbk' codec can't encode character '\u22ef'
在pycharm上正常运行
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-2-12 19:33:07 | 显示全部楼层
感谢分享
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-12-27 23:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表