飘忽若月 发表于 2022-3-7 12:57:29

正则表达式之疑惑求解

import re
import requests
import csv
import time

def request_pares(page):
    url = f'https://search.51job.com/list/010000,000000,0000,00,9,99,%25E5%25A4%25A7%25E6%2595%25B0%25E6%258D%25AE,2,{page}.html'
    #请求
    resp**e = requests.get(url=url, headers=headers)
    #解析提取
    job_name_list = re.findall('"job_name":"(.*?)","job_title":"', resp**e.text)
    company_name_list = re.findall('"company_name":"(.*?)","providesalary_text"', resp**e.text)
    salary_list = re.findall('"providesalary_text":"(.*?)","workarea"', resp**e.text)
    time.sleep(1)
    print(f'正在解析下载第(page)页招聘信息。。。。。。')
    download_save_csv(job_name_list,company_name_list,salary_list)


def download_save_csv(job_name_list,company_name_list,salary_list):
    for i in range(len(job_name_list)):
      data = , company_name_list, salary_list.replace('\/', '/')]
      with open('./data/职位信息.csv','a',encoding='utf-8',newline='') as f:
            writer = csv.writer(f)
            writer.writerow(data)

if __name__ == "__main__":
    withopen('./data/职位信息.csv','w',encoding='utf-8',newline='') as f:
      writer = csv.writer(f)
      writer.writerow(['职位','公司名称 ','薪资'])
      headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'
      }
      for page in range(1,21):
            request_pares(page)运行时出现如下错误:

File "C:\Users\Administrator\Desktop\正则表达式练习之爬取网站多页信息.py", line 27, in <module>
    withopen('./data/职位信息.csv','w',encoding='utf-8',newline='') as f:
FileNotFoundError: No such file or directory: './data/职位信息.csv'


有路过的大神帮忙分析一下吗?

就这名了儆 发表于 2022-3-7 12:57:50

和正则表达式无关,FileNotFoundError是在调用文件时path不存在文件导致的,设立路径时请确认path除了最后一个名字以外其他文件夹都存在,比如这个path='./data/职位信息.csv',要确定文件夹data确实存在才行
页: [1]
查看完整版本: 正则表达式之疑惑求解