TypeError: write() argument must be str, not dict

在写入文件的时候,出现:TypeError: write() argument must be str, not dict,报错。
可以使用json,格式写入

import requests
import re
import json

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

def parse_one_page(html):
    pattern = re.compile(
        'class="movie-item-info">.*?(.*?).*?class="star">(.*?)

.*?class="releasetime">(.*?)

', re.S) items = re.findall(pattern, html) for item in items: print(item) yield { 'title': item[0], 'mes': item[1].strip(), '_time': item[2] } def write_to_file(content): with open('result.txt','a',encoding='utf-8') as f: f.write(json.dumps(content,ensure_ascii=True)) def main(): url = 'https://maoyan.com/board/4' html = get_one_page(url) for item in parse_one_page(html): write_to_file(item) if __name__ == '__main__': main()

你可能感兴趣的:(爬虫,报错)