爬虫学习:Requests+正则表达式爬取猫眼电影

B站视频av19057145
`P14 requests+正则表达式
爬取网站信息代码:
(之前没有添加header时失败)

import requests
from requests.exceptions import RequestException
def get_one_page(url):
    try:
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36'
        }
        response = requests.get(url, headers=headers)
        if response.status_code == 200:
            return response.text
        return None
    except RequestException:
        return None
def main():
    url = 'https://maoyan.com/board/4'
    html = get_one_page(url)
    print(html)
if __name__ == '__main__':
    main()

在简单的对数据进行归纳整理后的代码如下:

import requests
from requests.exceptions import RequestException
import  re
    
def get_one_page(url):
    try:
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36'
        }
        response = requests.get(url, headers=headers)
        if response.status_code == 200:
            return response.text
        return None
    except RequestException:
        return None


def parse_one_page(html):
    pattern=re.compile('
.*?board-index.*?>(\d+).*?data-src="(.*?)".*?name">(.*?).*?star">(.*?)

.*?releasetime">(.*?)

' +'.*?integer">(.*?).*?fraction">(.*?).*?
',re.S) items=re.findall(pattern,html) 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] } #print(items) def main(): url = 'https://maoyan.com/board/4' html = get_one_page(url) for item in parse_one_page(html): print(item) if __name__ == '__main__': main()

在线程池中创造多进程爬取数据(会出现顺序不一致,初步估计是多进程状态下造成顺序混乱):

   import requests
    from multiprocessing import Pool
    import json
    from requests.exceptions import RequestException
    import re
    
    
    #获取网页信息
    def get_one_page(url):
        try:
            headers = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36'
            }
            response = requests.get(url, headers=headers)
            if response.status_code == 200:
                return response.text
            return None
        except RequestException:
            return None
    
    
    #抓取网页中需要的信息
    #圆括号中代表我们需要抓取的数据
    #需要熟知re库中compile和findall方法
    def parse_one_page(html):
        pattern=re.compile('
.*?board-index.*?>(\d+).*?data-src="(.*?)".*?name">(.*?).*?star">(.*?)

.*?releasetime">(.*?)

' +'.*?integer">(.*?).*?fraction">(.*?).*?
',re.S) items=re.findall(pattern,html) #建立字典结构使其规则输出 [x:]的意思是删除item[y]组中前x个字符 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] } #print(items) #写入到文件 def write_to_file(content): 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) for item in parse_one_page(html): print(item) write_to_file(item) if __name__ == '__main__': pool = Pool() pool.map(main,[i*10 for i in range(10)])

你可能感兴趣的:(爬虫学习:Requests+正则表达式爬取猫眼电影)