Python爬取前程无忧网址,并保存为txt文件


 1 import urllib.request
 2 import re
 3 
 4 
 5 def get_html(page):                 #获取网页的HTML
 6     url = 'http://search.51job.com/jobsearch/search_result.php?fromJs=1&jobarea=000000%2C00&district=000000&funtype=0000&industrytype=00&issuedate=9&providesalary=99&keyword=Python&keywordtype=2&curr_page=2&lang=c&stype=1&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&lonlat=0%2C0&radius=-1&ord_field=0&list_type=0&fromType=14&dibiaoid=0&confirmdate=9'.format(page)
 7     html = urllib.request.urlopen(url).read()
 8     html = html.decode('gbk')
 9     return html
10 
11 def get_you_want(html):         #获取需要的征文内容
12     reg = re.compile(r'class="t1 ">.*?<a target="_blank" title="(.*?)".*?<span class="t2"><a target="_blank" title="(.*?)".*?<span class="t3">(.*?)</span>.*?<span class="t4">(.*?)</span>',re.S)
13     you_want = re.findall(reg,html)
14     print(you_want)
15     return you_want
16 
17 for get_page in range(1,6):
18     htmls = get_html(get_page) #调用获取源码
19     for i in get_you_want(htmls):
20         print(i[0],i[1],i[2],i[3])
21         with open('前程无忧.txt','a') as f:
22             f.write(i[0]+'\t'+i[1]+'\t'+i[2]+'\t'+i[3]+'\n\n')

 



我用的是Python 3 其实和Python 2 差不多 ,就是Python3没有了urllib2,需要导入import urllib.requests来代替import urllib2,其他的都差不多


PS:
  其实我是想保存为Excel来进行后续的分析的,但是发现比较麻烦就放弃了!(此处应该有表情的◔ ‸◔?)


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM