Python爬虫——豆瓣电影Top250

本文介绍了一个使用Python的urllib和BeautifulSoup库来爬取豆瓣电影Top250榜单的简单程序。该程序能够自动翻页并抓取所有250部电影的名字,最终将这些数据保存到本地文件中。
#爬取 豆瓣电影Top250
#250个电影 ,分为10个页显示,1页有25个电影

import urllib.request
from bs4 import BeautifulSoup

url = "http://movie.douban.com/top250"
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko'}
#headers 要根据自己的网页抓取情况修改

targetPath = "storage path"  #填写自己想要存储的地址

def saveText(f,text):
    f.write(text)
#获取网页源码
def getData(url,headers):
    req = urllib.request.Request(url = url , headers = headers)
    res = urllib.request.urlopen(req)
    data = res.read()
    return data

#解析网页
def praseHtml(f,url,headers):
    currenturl = url
    i = 1 #序号
    #flag = True
    while currenturl :
        #解析当前页,获取想要的内容
        html = getData(currenturl,headers)

        soup =  BeautifulSoup(html,'lxml')

        moveList = soup.find('ol',attrs = {'class':'grid_view'})

        for moveLi in moveList.find_all('li'):
            detail = moveLi.find('div',attrs = {'class':'hd'})
            moveName = detail.find('span',attrs = {'class':'title'})
            saveText(f,str(i)+ moveName.getText()+'\n')
            i += 1
            print(moveName.getText())

        #下一页
        nextpage = soup.find('span',attrs = {'class':'next'}).find('a')
        #next = nextpage['href']   #这样写报错:NoneType object is not subscriptable
        if nextpage:
            currenturl = url + nextpage['href']
        else :
            currenturl = None


f = open(targetPath,"w")
praseHtml(f,url,headers)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值