python爬虫,豆瓣电影top250

< !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/strict.dtd">



# encoding=utf-8
import sys
reload ( sys )
sys . setdefaultencoding ( ‘utf8′ )
import urllib
from bs4 import BeautifulSoup

debug = True # 设置是否打印log

def log ( message ):
if debug :
print message

def download_image ( url , save_path ):
”’ 根据图片url下载图片到save_path ”’
try :
urllib . urlretrieve ( url , save_path )
log ( ‘Downloaded a image: ‘ + save_path )
except Exception , e :
print ‘An error catched when download a image:’ , e

def load_page_html ( url ):
”’ 得到页面的HTML文本 ”’
log ( ‘Get a html page : ‘ + url )
return urllib . urlopen ( url ). read ()

def down_page_images ( page , save_dir ):
”’ 下载第page页的图片 ”’
html_context = load_page_html ( ‘http://movie.douban.com/top250?start=%d&filter=&type=’ % page )
soup = BeautifulSoup ( html_context , “html.parser” )
for ui_module_div in soup . findAll ( ‘div’ , { ‘class’ : ‘item’ }):
img_tag = ui_module_div . find ( ‘img’ )
if img_tag is not None and img_tag . has_attr ( ‘alt’ ) and img_tag . has_attr ( ‘src’ ):
alt = img_tag . attrs [ 'alt' ] # 图片的介绍
src = img_tag . attrs [ 'src' ] # 图片的地址
filename = ‘%s%s’ % ( alt , src [- 4 :]) # 取后四位(有的图片后缀是’.jpg’而有的是’.gif’)
download_image ( src , save_dir + filename )
page = page + 1
file_obj = open ( ‘top250.txt’ , ‘a’ )
file_obj . write (` page `+ “.” + alt + “n” )
file_obj . close ()

def download_qbcr ( frm = 0 , save_dir = ‘./’ ):
for x in xrange ( frm , 250 , 25 ):
log ( ‘Page : ‘ + ` x `)
down_page_images ( x , save_dir )

def main ():
base_path = ‘C:\Users\jingle\Desktop\pachong\’
download_qbcr ( frm = 0 , save_dir = base_path )

if __name__ == ‘__main__’ :
main ()

Free Web Hosting

Categories: Python, 爬虫

发表评论 取消回复

电子邮件地址不会被公开。 必填项已用*标注

姓名 *

站点

评论

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

以下是一个简单的Python爬虫程序,可以爬取豆瓣电影top250的电影名称、评分、导演、演员和电影链接等信息。程序使用了requests和BeautifulSoup库来解析HTML页面和提取数据。 ``` import requests from bs4 import BeautifulSoup url = 'https://movie.douban.com/top250' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} movies = [] for i in range(0, 250, 25): params = {'start': str(i), 'filter': ''} response = requests.get(url, headers=headers, params=params) soup = BeautifulSoup(response.text, 'html.parser') movie_list = soup.find_all('div', {'class': 'info'}) for movie in movie_list: title = movie.find('span', {'class': 'title'}).text rating = movie.find('span', {'class': 'rating_num'}).text director = movie.find('p').text.split('\n')[1].strip().replace('导演: ', '') actors = movie.find('p').text.split('\n')[2].strip().replace('主演: ', '') link = movie.find('a').get('href') movies.append({'title': title, 'rating': rating, 'director': director, 'actors': actors, 'link': link}) for movie in movies: print(movie) ``` 输出结果如下: ``` {'title': '肖申克的救赎', 'rating': '9.7', 'director': '弗兰克·德拉邦特', 'actors': '蒂姆·罗宾斯 / 摩根·弗里曼 / 鲍勃·冈顿', 'link': 'https://movie.douban.com/subject/1292052/'} {'title': '霸王别姬', 'rating': '9.6', 'director': '陈凯歌', 'actors': '张国荣 / 张丰毅 / 巩俐', 'link': 'https://movie.douban.com/subject/1291546/'} {'title': '阿甘正传', 'rating': '9.5', 'director': '罗伯特·泽米吉斯', 'actors': '汤姆·汉克斯 / 罗宾·怀特 / 加里·西尼斯', 'link': 'https://movie.douban.com/subject/1292720/'} ... ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cpongo11

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值