weixin_56906252 2021-08-01 12:32 采纳率: 75%
浏览 366
已结题

为什么用python 爬取数据到excel中300多行只剩下一行?

用vscode 爬取了招聘网站上的信息,一共有300行,可是打开excel只有一行,是哪里出了问题?

'''
https://search.51job.com/list/000000,000000,0000,00,9,99,%25E5%25A4%25A7%25E6%2595%25B0%25E6%258D%25AE,2,1.html?lang=c&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&ord_field=0&dibiaoid=0&line=&welfare=
https://search.51job.com/list/000000,000000,0000,00,9,99,%25E5%25A4%25A7%25E6%2595%25B0%25E6%258D%25AE,2,2.html?lang=c&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&ord_field=0&dibiaoid=0&line=&welfare=
'''
import requests
import re
import json
headers={
'Cookie': 'guid=14c32c5dce04fa3d6d4f0dc8961c9aab; _ujz=MTk0NzEyODA5MA%3D%3D; ps=needv%3D0; 51job=cuid%3D194712809%26%7C%26cusername%3DB7KK9VNsOn8NSWFm23ROeZ6h9iUldHv%252FMqcnazEnEJM%253D%26%7C%26cpassword%3D%26%7C%26cname%3Dt9B%252BNKDJvB%252BWNuNHKMPOLw%253D%253D%26%7C%26cemail%3DFMUVanQ9aK11RLniOX4AV63V44VqRqtmEVhlmwp8AX8%253D%26%7C%26cemailstatus%3D0%26%7C%26cnickname%3D%26%7C%26ccry%3D.010VOsXHMfEk%26%7C%26cconfirmkey%3D%25241%2524dktOIKCx%2524rKLClGXGDScO4sXbAjpDe%252F%26%7C%26cautologin%3D1%26%7C%26cenglish%3D0%26%7C%26sex%3D0%26%7C%26cnamekey%3D%25241%2524E2r2iH54%2524I.ka46kLArC3hr7n3liJ90%26%7C%26to%3Ddcf678fb66c38a0100e0ed04388372d760f7debc%26%7C%26; nsearch=jobarea%3D%26%7C%26ord_field%3D%26%7C%26recentSearch0%3D%26%7C%26recentSearch1%3D%26%7C%26recentSearch2%3D%26%7C%26recentSearch3%3D%26%7C%26recentSearch4%3D%26%7C%26collapse_expansion%3D; ssxmod_itna=QqRx2D97GQKGqBKDQDXDn7AEPRxfxRrSx3uAiqGNqN3DZDiqAPGhDC+b9bm8r82wmYE4tiwvxYbgDhpaYa42wToEfmx0aDbqGkqFAxQGGjxBYDQxAYDGDDPkDj4ibDYbU7DjjtzZMLdqiOD7eDXxGCDQKGVxDaDGc8oen8nEphD+YDn=D1K708D75Dux0HzifeLxDCpzfeSr5vz3u4iDGd=7LxF7dYFiDDEmbOebYDvxDkd35HY8LklOv7DywTd7xPajGqWQi3kNGqcSDxorx4iGG5Qx2X8Dxe/eNiiAr99dxDiO0YykGDD=; ssxmod_itna2=QqRx2D97GQKGqBKDQDXDn7AEPRxfxRrSx3uADA=nppxD/lmFDFxO77IxuPAPXOznQBEvqG4hnuGPGQ=mQY=zYRhAy44=ux1GWvCp/MgawTLfmEX6PcIAH8mHvoO=1qgmDpITqR=tl6OG76GIx5yBgtSerEgIhAexStDwnt5ChUYxErMmA2Y1wtMjfiMYDuwIhD5t=T8t=bzC+nM3CjQvQoCsexLC6UWqc25icFsqFyGN99m567hUCHK+KAsKG8ONLbGOUBMjD+Hm9usX=7j14R8VSuOkvk9PZjQNQOihb2O8xct1oVNlyQbHwQiUW4=7xh7G/R+03mPUhdKW+AqF4A8CT6Uqr/aX4Y1ta/3AvtxiterjiO3B8I9rC9qnBGKu4lGWQiqT4d3u4zEPAvx=0Yt4t1hNpm7jbHmWZCx=H8tUnTmndEb+ImaFYPHTOCH8AR7faC3W9inePqWbqTyFHic1zxNtWtGetjqHmm9/2ZSxtnM7XwqixDKuTD7=DYIeeD==; partner=sem_pc360s3_103928; slife=lastlogindate%3D20210801%26%7C%26; search=jobarea%7E%60000000%7C%21ord_field%7E%600%7C%21recentSearch0%7E%60000000%A1%FB%A1%FA000000%A1%FB%A1%FA0000%A1%FB%A1%FA00%A1%FB%A1%FA99%A1%FB%A1%FA%A1%FB%A1%FA99%A1%FB%A1%FA99%A1%FB%A1%FA99%A1%FB%A1%FA99%A1%FB%A1%FA9%A1%FB%A1%FA99%A1%FB%A1%FA%A1%FB%A1%FA0%A1%FB%A1%FA%B4%F3%CA%FD%BE%DD%A1%FB%A1%FA2%A1%FB%A1%FA1%7C%21recentSearch1%7E%60000000%A1%FB%A1%FA000000%A1%FB%A1%FA0000%A1%FB%A1%FA00%A1%FB%A1%FA99%A1%FB%A1%FA%A1%FB%A1%FA99%A1%FB%A1%FA99%A1%FB%A1%FA99%A1%FB%A1%FA99%A1%FB%A1%FA9%A1%FB%A1%FA99%A1%FB%A1%FA%A1%FB%A1%FA0%A1%FB%A1%FA%B4%F3%CA%FD%BE%DD%BF%AA%B7%A2%A1%FB%A1%FA2%A1%FB%A1%FA1%7C%21recentSearch2%7E%60000000%A1%FB%A1%FA000000%A1%FB%A1%FA0000%A1%FB%A1%FA00%A1%FB%A1%FA99%A1%FB%A1%FA%A1%FB%A1%FA99%A1%FB%A1%FA99%A1%FB%A1%FA99%A1%FB%A1%FA99%A1%FB%A1%FA9%A1%FB%A1%FA99%A1%FB%A1%FA%A1%FB%A1%FA0%A1%FB%A1%FA%B4%F3%CA%FD%BE%DD%B7%D6%CE%F6%CA%A6%A1%FB%A1%FA2%A1%FB%A1%FA1%7C%21recentSearch3%7E%60000000%A1%FB%A1%FA000000%A1%FB%A1%FA0000%A1%FB%A1%FA00%A1%FB%A1%FA99%A1%FB%A1%FA%A1%FB%A1%FA99%A1%FB%A1%FA99%A1%FB%A1%FA99%A1%FB%A1%FA99%A1%FB%A1%FA9%A1%FB%A1%FA99%A1%FB%A1%FA%A1%FB%A1%FA0%A1%FB%A1%FApython%A1%FB%A1%FA2%A1%FB%A1%FA1%7C%21recentSearch4%7E%60210200%2C100200%A1%FB%A1%FA000000%A1%FB%A1%FA0000%A1%FB%A1%FA00%A1%FB%A1%FA99%A1%FB%A1%FA%A1%FB%A1%FA99%A1%FB%A1%FA99%A1%FB%A1%FA99%A1%FB%A1%FA99%A1%FB%A1%FA9%A1%FB%A1%FA99%A1%FB%A1%FA%A1%FB%A1%FA0%A1%FB%A1%FA%B4%F3%CA%FD%BE%DD%B7%D6%CE%F6%CA%A6%A1%FB%A1%FA2%A1%FB%A1%FA1%7C%21; adv=ad_logid_url%3Dhttps%253A%252F%252Ftrace.51job.com%252Ftrace.php%253Fpartner%253Dsem_pc360s3_103928%2526ajp%253DaHR0cHM6Ly9ta3QuNTFqb2IuY29tL3RnL3NlbS9MUF8yMDIwXzEuaHRtbD9mcm9tPTM2MGFk%2526k%253D7d16490a53bc7f778963fbe04432456c%2526qhclickid%253D2cd296953e58ae6a%26%7C%26',
 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36 Edg/92.0.902.62'
}
for page in range(1,301): 
    url='https://search.51job.com/list/000000,000000,0000,00,9,99,%25E5%25A4%25A7%25E6%2595%25B0%25E6%258D%25AE,2,{}.html?lang=c&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&ord_field=0&dibiaoid=0&line=&welfare='.format(page)
    print(url)
    response =requests. get (url, headers=headers).text
    r=re.findall('window.__SEARCH_RESULT__ = (.*?)</script>',response,re.S)
    string =''.join(r)
    infodict=json.loads(string)
    engine_search_result=infodict['engine_search_result']  
    for i in engine_search_result:
     job_name=i['job_name']
     company_name=i['company_name']
    providesalary_text=i['providesalary_text']
    workarea_text=i['workarea_text']

f=open('51job.csv','a')
f.write('{},{},{},{}\n'.format(job_name,company_name,providesalary_text,workarea_text))
f.close()


这是打开excel的样子

img

请问这是那一步出现了问题?

  • 写回答

4条回答 默认 最新

  • CSDN专家-HGJ 2021-08-01 13:32
    关注

    将写入文件语句放到循环外,加上列名并逐行写入,后面的代码可以这样写:

    #code
    with open('data/51job.csv','w') as f:
        f.write(','.join(['job_name', 'company_name',  'providesalary', 'workarea'])+'\n')
        for page in range(1,21): 
            url='https://search.51job.com/list/000000,000000,0000,00,9,99,%25E5%25A4%25A7%25E6%2595%25B0%25E6%258D%25AE,2,{}.html?lang=c&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&ord_field=0&dibiaoid=0&line=&welfare='.format(page)
            print(url)
            response =requests. get (url, headers=headers).text
            r=re.findall('window.__SEARCH_RESULT__ = (.*?)</script>',response,re.S)
            string =''.join(r)
            infodict=json.loads(string)
            engine_search_result=infodict['engine_search_result']  
            for i in engine_search_result:
                job_name=i['job_name']
                company_name=i['company_name']
                providesalary_text=i['providesalary_text']
                workarea_text=i['workarea_text']
                
                f.write('{},{},{},{}\n'.format(job_name,company_name,providesalary_text,workarea_text))
    
    

    若有帮助,请点采纳。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 12月13日
  • 已采纳回答 12月5日
  • 创建了问题 8月1日

悬赏问题

  • ¥15 应用简单的Python代码完成一个学生成绩管理系统
  • ¥15 用matlab求微分方程初值问题
  • ¥15 vscode下编写第三方库opencv与pcl代码时没有代码提示
  • ¥15 能够跑通不报错,如何解决?(标签-matlab)
  • ¥15 MOS在RDS较大,频率高时开关波形异常
  • ¥15 SCENIC分析报错求解答
  • ¥15 ceph初始化mon不成功 下图不报错 这个是什么元
  • ¥15 数学建模数学建模少ai
  • ¥35 这个等效电路图是什么意思?(答疑)(有没有不是chat的,我就是在chat搜不到才来问的,chat还开了VIP)(可以追加酬金)
  • ¥200 基于同花顺supermind的量化策略脚本编辑