python爬取猫眼电影top100排行榜

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python爬取猫眼电影top100排行榜相关的知识,希望对你有一定的参考价值。

爬取猫眼电影TOP100(http://maoyan.com/board/4?offset=90)
1). 爬取内容: 电影名称,主演, 上映时间,图片url地址保存到mariadb数据库中;
2). 所有的图片保存到本地/mnt/maoyan/电影名.png

代码:


import re
import pymysql as mysql
from urllib import request
from urllib.request import urlopen

u = ‘root‘
p = ‘root‘
d = ‘python‘
sql = ‘insert into maoyan_top100 values(%s,%s,%s,%s,%s)‘

url = ‘http://maoyan.com/board/4?offset=‘
pattern = r‘<dd>[sS]*?board-index.*?>(d+)</i>[sS]*?<img data-src="(http://.+?)" alt="(.*?)"[sS]*?star">[s]*(.*?)[s]*?</p>[sS]*?releasetime">[s]*(.*?)[s]*?</p>‘
myAgent = "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0"

conn = mysql.connect(user=u, passwd=p, db=d, charset=‘utf8‘, autocommit=True)
cur = conn.cursor()

def write_to_mysql(item):
    cur.executemany(sql,item)

def save_picture(rank,img_url,film_name):
    img_content = urlopen(img_url).read()
    img_name = ‘maoyan_images/‘+rank+‘_‘+film_name+‘.jpg‘
    with open(img_name,‘wb‘) as f:
        f.write(img_content)

def main():
    for i in range(10):
        page_url = url+str(i*10)
        myrequest = request.Request(page_url, headers={‘User-Agent‘: myAgent})
        page_content = urlopen(myrequest).read().decode(‘utf-8‘)
        items = re.findall(pattern,page_content)
        # [(‘1‘, ‘http://p1.meituan.net/movie/[email protected]_220h_1e_1c‘, ‘霸王别姬‘, ‘主演:张国荣,张丰毅,巩俐‘, ‘上映时间:1993-01-01‘)...]
        write_to_mysql(items)
        for item in items:
            save_picture(item[0],item[1],item[2])

if __name__ == ‘__main__‘:
    main()

爬取结果:
1)保存海报图片
技术分享图片
2)数据库数据
技术分享图片

以上是关于python爬取猫眼电影top100排行榜的主要内容,如果未能解决你的问题,请参考以下文章

Python3编写网络爬虫04-爬取猫眼电影排行实例

python爬取猫眼电影top100

python爬取猫眼电影的Top100

抓取猫眼top100电影信息

用Python爬取猫眼上的top100评分电影

Python爬取猫眼top100排行榜数据含多线程