Python爬虫:requests多进程爬取猫眼电影榜单

简介: Python爬虫:requests多进程爬取猫眼电影榜单

思路:

抓取单页 -> 解析信息 -> 保存文件 -> 多线程循环

TOP100榜单地址:http://maoyan.com/board/4


代码实现

# 爬取猫眼电影榜单
import time
import json
import requests
from pyquery import PyQuery
from multiprocessing import Pool
from requests.exceptions import RequestException
def get_one_page(url):
    # 获取一个页面
    headers = {"User-Agent": "Mozilla/5.0"}
    try:
        response = requests.get(url, headers=headers)
        if response.status_code != 200:
            return None
    except RequestException:
        return None
    return response.text
def pase_one_page(text):
    # 解析页面内容
    doc = PyQuery(text)
    for info in doc("dl.board-wrapper dd").items():
        dct = {}
        dct["index"] = info.find(".board-index").text()
        dct["name"] = info.find("p.name a").text()
        dct["star"] = info.find("p.star").text()
        dct["releasetime"] = info.find("p.releasetime").text()
        dct["score"] = info.find(".score").text()
        yield dct
def write_to_file(content):
    # 写入文件
    with open("data.txt", "a", encoding="utf-8") as f:
        f.write(json.dumps(content, ensure_ascii=False)+"\n")
def main(offset):
    # 程序入口
    url = "http://maoyan.com/board/4?offset={offset}"
    text = get_one_page(url.format(offset=offset))
    for item in pase_one_page(text):
        write_to_file(item)
if __name__ == "__main__":
    start = time.time()
    # 循环抓取,翻页
    # for i in range(10):
    #     main(i * 10)
    # 3.06 6.18 4.12 3.68 3.98
    # 多进程抓取,翻页
    pool = Pool()
    pool.map(main, [i*10 for i in range(10)])
    end = time.time()
    print(end-start)
    # 0.67 0.68 0.67 1.82 0.64
相关文章
|
4月前
|
数据采集 Web App开发 监控
Python爬虫:爱奇艺榜单数据的实时监控
Python爬虫:爱奇艺榜单数据的实时监控
|
4月前
|
JSON 数据可视化 测试技术
python+requests接口自动化框架的实现
通过以上步骤,我们构建了一个基本的Python+Requests接口自动化测试框架。这个框架具有良好的扩展性,可以根据实际需求进行功能扩展和优化。它不仅能提高测试效率,还能保证接口的稳定性和可靠性,为软件质量提供有力保障。
214 7
|
6月前
|
数据采集 前端开发 算法
Python Requests 的高级使用技巧:应对复杂 HTTP 请求场景
本文介绍了如何使用 Python 的 `requests` 库应对复杂的 HTTP 请求场景,包括 Spider Trap(蜘蛛陷阱)、SESSION 访问限制和请求频率限制。通过代理、CSS 类链接数控制、多账号切换和限流算法等技术手段,提高爬虫的稳定性和效率,增强在反爬虫环境中的生存能力。文中提供了详细的代码示例,帮助读者掌握这些高级用法。
424 1
Python Requests 的高级使用技巧:应对复杂 HTTP 请求场景
|
5月前
|
数据可视化 搜索推荐 Shell
Python与Plotly:B站每周必看榜单的可视化解决方案
Python与Plotly:B站每周必看榜单的可视化解决方案
|
6月前
|
网络协议 数据库连接 Python
python知识点100篇系列(17)-替换requests的python库httpx
【10月更文挑战第4天】Requests 是基于 Python 开发的 HTTP 库,使用简单,功能强大。然而,随着 Python 3.6 的发布,出现了 Requests 的替代品 —— httpx。httpx 继承了 Requests 的所有特性,并增加了对异步请求的支持,支持 HTTP/1.1 和 HTTP/2,能够发送同步和异步请求,适用于 WSGI 和 ASGI 应用。安装使用 httpx 需要 Python 3.6 及以上版本,异步请求则需要 Python 3.8 及以上。httpx 提供了 Client 和 AsyncClient,分别用于优化同步和异步请求的性能。
145 1
python知识点100篇系列(17)-替换requests的python库httpx
|
5月前
|
数据采集 JSON 测试技术
Python爬虫神器requests库的使用
在现代编程中,网络请求是必不可少的部分。本文详细介绍 Python 的 requests 库,一个功能强大且易用的 HTTP 请求库。内容涵盖安装、基本功能(如发送 GET 和 POST 请求、设置请求头、处理响应)、高级功能(如会话管理和文件上传)以及实际应用场景。通过本文,你将全面掌握 requests 库的使用方法。🚀🌟
855 7
|
5月前
|
机器学习/深度学习 数据采集 搜索推荐
利用Python和机器学习构建电影推荐系统
利用Python和机器学习构建电影推荐系统
238 1
|
6月前
|
存储 网络协议 API
详解Python中的Requests会话管理
详解Python中的Requests会话管理
|
6月前
|
监控 安全 中间件
Python requests 如何避免被 Gzip 炸弹攻击
Python requests 如何避免被 Gzip 炸弹攻击
79 0
|
6月前
|
Python 容器
AutoDL Python实现 自动续签 防止实例过期释放 小脚本 定时任务 apscheduler requests
AutoDL Python实现 自动续签 防止实例过期释放 小脚本 定时任务 apscheduler requests
140 0