python网络爬虫--正则表达式抓取猫眼电影排行TOP100

news/2024/7/19 11:25:08 标签: 爬虫, python, json

本篇随笔,记录学习崔庆才老师编著的《Python3 网络爬虫实战开发》

打开猫眼电影,找到TOP100榜,观察URL连接。发现offset字段变化,其它都不变。offset字段即电影数量的偏移量。

# 使用requests第三方请求库
import
requests import re import json def get_one_page(url): headers = { "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36" } response = requests.get(url,headers = headers) if response.status_code == 200: return response.text return None def parse_one_page(html): pattern = re.compile('<dd>.*?board-index.*?>(.*?)</i>.*?data-src="(.*?)".*?name.*?a.*?>(.*?)</a>.*?star.*?>(.*?)</p>.*?releasetime.*?>(.*?)</p>.*?integer.*?>(.*?)</i>.*?fraction.*?>(.*?)</i>.*?</dd>',re.S) items = re.findall(pattern,html) for item in items: yield{ "index":item[0], "image":item[1], "title":item[2].strip(), "actor":item[3].strip()[3:] if len(item[3])>3 else "", "time":item[4].strip()[5:] if len(item[4])>5 else "", "score":item[5].strip() + item[6].strip() } def write_to_file(content): with open("result.text","a",encoding = "utf-8") as f: print(type(json.dumps(content))) f.write(json.dumps(content,ensure_ascii = False)+"\n") def main(offset): url = "https://maoyan.com/board/4?offset="+str(offset) html = get_one_page(url) for item in parse_one_page(html): write_to_file(item) if __name__ == "__main__": for i in range(10): main(offset = i*10)

 

# 使用urllib内置请求库
from urllib import request
import re
import json


def get_one_page(url):
    headers = {
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"
        }
    my_request = request.Request(url,headers = headers)
    response = request.urlopen(my_request)
    if response.code == 200:
        return response.read()
    return None


def parse_one_page(html):
    pattern = re.compile('<dd>.*?board-index.*?>(.*?)</i>.*?data-src="(.*?)".*?name.*?a.*?>(.*?)</a>.*?star.*?>(.*?)</p>.*?releasetime.*?>(.*?)</p>.*?integer.*?>(.*?)</i>.*?fraction.*?>(.*?)</i>.*?</dd>',re.S)
    items = re.findall(pattern,html)
    for item in items:
        yield{
            "index":item[0],
            "image":item[1],
            "title":item[2].strip(),
            "actor":item[3].strip()[3:] if len(item[3])>3 else "",
            "time":item[4].strip()[5:] if len(item[4])>5 else "",
            "score":item[5].strip() + item[6].strip()
            }

def write_to_file(content):
    with open("result.text","a",encoding = "utf-8") as f:
        print(type(json.dumps(content)))
        f.write(json.dumps(content,ensure_ascii = False)+"\n")      


def main(offset):
    url = "https://maoyan.com/board/4?offset="+str(offset)
    html = get_one_page(url)
    for item in parse_one_page(html):
        write_to_file(item)

if __name__ == "__main__":
    for i in range(10):
        main(offset = i*10)

 

转载于:https://www.cnblogs.com/sakura-d/p/10894994.html


http://www.niftyadmin.cn/n/782668.html

相关文章

qt xml组包_利用 Qt 读取 XML 文件的方法

XML 是可扩大标记语言(Extensible Markup Language)的缩写。XML文件由内容和标记组成&#xff0c;通过以标记包围内容的方式将大部份内容包括在元素中。Qt 中提供了多种读取XML文件的方法&#xff0c;这里简单的记录1下用QDomDocument读取的步骤。为何使用QDomDocument呢&#…

ucos操作系统(2)——OSTimeDly

时间管理函数有OSTimeDly()和OSTimeDlyHMSM() 以前者为例&#xff0c;调用OSTimeDly()时&#xff1a; 1.取消当前任务的就绪态&#xff0c;使之进入等待态&#xff1b; 2.给OSTCBDly赋值&#xff0c;即将参数传至OSTCBDly&#xff0c;就是在等待态的时间&#xff1b; 3.调用函数…

面试问题_六个问题,让面试官不再犹豫

找工作面试时&#xff0c;除了被面试官问各种问题&#xff0c;面试者也可以大胆提出自己的问题。主动提问&#xff0c;既可以表示你对这份工作很有兴趣&#xff0c;想多了解&#xff0c;又可以让你知道这份工作更多的信息。但怎么提出合适的问题呢&#xff1f;新媒体商业内幕网…

ucos操作系统(1)——OSTCBY,OSRdyGrp,OSRdyTbl

OSRdyGrp相当于列&#xff0c;有多少组&#xff0c;是一个变量 OSRdyTbl是一个数组&#xff0c;每个数组元素是8位的&#xff0c;每一位代表一个优先级 转载自https://blog.csdn.net/d521000121/article/details/53678252 关于这三个系统变量&#xff0c;研究了好一阵子&…

扩展KMP Sunday算法

刷题刷到kmp了 TLE了 想着还是sunday算法高效一点 还好写 kmp和扩展kmp的思想都是在不匹配时如何高效移动原串的匹配起点sunday直接判断步长为模式串的长度&#xff0c;效率最高原串 s&#xff1a;fffsffkfsfff size n12 匹配起点为i模式串 p&#xff1a;fsf size m3每次匹配 …

linux 换行符 替换_Linux生信常用快速操作

基因测序的结果非常大&#xff0c;介绍一些直接在Linux服务器上操作的快速方法。大家应该掌握基本的管道操作&#xff0c;for循环&#xff0c;以及zcat、cat、less、grep、cut、sed、paste、vi等命令。根据数值、字符或者字符长度筛选行Filter rows by column value# 保留第7列…

PID算法 c语言实现,简单易懂

https://blog.csdn.net/wzhcalex/article/details/69944223 #include <stdio.h> struct _pid {float SetSpeed; //定义设定值float ActualSpeed; //定义实际值float err; //定义偏差值float err_last; //定义上一个偏差值float Kp,Ki,Kd…

Decorator——Python初级函数装饰器

最近想整一整数据分析&#xff0c;在看一本关于数据分析的书中提到了(1)if __name__ __main__ (2)列表解析式 (3)装饰器。 先简单描述一下前两点&#xff0c;再详细解说Python初级的函数装饰器。 进入正题&#xff1a; 一、if __name__ __main__ 首先&#xff0c;__name__是…