首页 > 编程语言 > 详细

python爬虫之爬取猫眼榜单

时间:2020-09-27 21:20:32      阅读:34      评论:0      收藏:0      [点我收藏+]

抓取“猫眼”TOP100榜

import requests
from bs4 import BeautifulSoup
headers = {
    user-agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)     Chrome/70.0.3538.102 Safari/537.36
}


def open_url(url):
    r = requests.get(url, headers=headers)
    r.encoding = utf-8
    html = r.text
    return html


def get_info(html):
    # num = 1
    soup = BeautifulSoup(html, lxml)
    text_info = soup.find_all(div, class_="movie-item-info")
    # for i in text_info:
    #     info = str(num) + ‘.‘ + i.text.replace(‘ ‘, ‘‘).replace(‘\n‘, ‘  ‘) + ‘\n‘
    #     print(info)
    #     num += 1
    return text_info


def main():
    num = 1
    for i in range(0, 10):
        url = https://maoyan.com/board/4?offset= + str(0+i*10)
        html = open_url(url)
        info_list = get_info(html)
        for info in info_list:
            info = str(num) + . + info.text.replace( , ‘‘).replace(\n,   ) + \n
            num += 1
            with open(猫眼TOP100榜.txt, a+) as file:
                file.write(info)
    print(猫眼TOP100榜下载完成!)


if __name__ == __main__:
    main()

python爬虫之爬取猫眼榜单

原文:https://www.cnblogs.com/jsxxd/p/13740903.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!