Requests+正则表达式爬取猫眼TOP100电影

需要着重学习的地方:(1)爬取分页数据时,url链接的构建(2)保存json格式数据到文件,中文显示问题(3)线程池的使用(4)正则表达式的写法
import requests
from requests.exceptions import RequestException
import re
import json
from multiprocessing import Pool

# 抓取单页内容
def get_one_page(url):
    try:
        response = requests.get(url)
        if response.status_code == 200:
            return response.text
        return None
    except RequestException:
        return  None

# 正则表达式分析
def parse_one_page(html):
    pattern = re.compile(‘<dd>.*?board-index.*?>(\d+)</i>.*?data-src="(.*?)".*?name"><a‘
                         +‘.*?>(.*?)</a>.*?star">(.*?)</p>.*?releasetime">(.*?)</p>‘
                         +‘.*?integer">(.*?)</i>.*?fraction">(.*?)</i>.*?</dd>‘,re.S)

    items = re.findall(pattern,html)
    # print(items)
    for item in items:
        yield {
            ‘index‘:item[0],
            ‘image‘:item[1],
            ‘title‘:item[2],
            ‘actor‘:item[3].strip()[3:],
            ‘time‘:item[4].strip()[5:],
            ‘score‘:item[5] + item[6]
        }

#  保存至文件
def write_to_file(content):
    # 注意:把json数据保存到文件中显示出中文
    with open(‘result.txt‘,‘a‘,encoding=‘utf-8‘) as f:
        f.write(json.dumps(content,ensure_ascii=False) + ‘\n‘)
        f.close()

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

if __name__ == ‘__main__‘:
    # 开启循环,抓取多页数据
    # for i in range(10):
    #     main(i*10)
    # 使用线程池
    pool = Pool()
    pool.map(main,[i*10 for i in range(10)])

 
 

原文地址:https://www.cnblogs.com/sanduzxcvbnm/p/10185364.html

时间: 2024-10-01 06:51:54

Requests+正则表达式爬取猫眼TOP100电影的相关文章

python爬虫知识点总结(九)Requests+正则表达式爬取猫眼电影

一.爬取流程 二.代码演示 #-*- coding: UTF-8 -*- #_author:AlexCthon #mail:[email protected] #date:2018/8/3 import requests from multiprocessing import Pool # 进程池,用来实现秒抓 from requests.exceptions import RequestException import re import json def get_one_page(url):

Python爬虫实战之Requests+正则表达式爬取猫眼电影Top100

import requests from requests.exceptions import RequestException import re import json # from multiprocessing import Pool # 测试了下 这里需要自己添加头部 否则得不到网页 headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Ge

Requests+正则表达式 爬取猫眼电影

代码: import re import json from multiprocessing import Pool import requests from requests.exceptions import RequestException basic_url = 'http://maoyan.com/board/4?offset=%d' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWe

Requests+正则表达式爬取猫眼电影

1 # encoding:utf-8 2 from requests.exceptions import RequestException 3 import requests 4 import re 5 import json 6 from multiprocessing import Pool 7 8 def get_one_page(url): 9 try: 10 response = requests.get(url) 11 if response.status_code == 200:

14-Requests+正则表达式爬取猫眼电影

'''Requests+正则表达式爬取猫眼电影TOP100''''''流程框架:抓去单页内容:利用requests请求目标站点,得到单个网页HTML代码,返回结果.正则表达式分析:根据HTML代码分析得到电影的名称.主演.上映时间.评分.图片链接等信息.保存至文件:通过文件的形式保存结果,每一部电影一个结果一行Json字符串.开启循环及多线程:对多页内容遍历,开启多线程提高抓取速度.'''import requestsimport refrom requests.exceptions impor

正则表达式爬取猫眼电影

正则表达式爬取猫眼电影Top100 import requests import re,json from multiprocessing import Pool def get_one_page(url): response = requests.get(url) return response.text def parse_one_page(html): pattern = re.compile('<dd>.*?board-index.*?>(\d+)</i>.*?dat

requests+正则表达式爬取ip

1 #requests+正则表达式爬取ip 2 #findall方法,如果表达式中包含有子组,则会把子组单独返回出来,如果有多个子组,则会组合成元祖 3 import requests 4 import re 5 def get_ip(url): 6 headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari

requests库爬取猫眼电影“最受期待榜”榜单 --网络爬虫

目标站点:https://maoyan.com/board/6 # coding:utf8 import requests, re, json from requests.exceptions import RequestException # from multiprocessing import Pool # 获取页面 def get_one_page(url): try: resp = requests.get(url) if resp.status_code == requests.co

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

# -*- coding: utf-8 -*- import requests from multiprocessing import Pool from requests.exceptions import RequestException import re import json def get_one_page(url): """ 爬取每个页面 :param url: 爬取url地址 :return: 返回网页内容 """ try: re