#coding=utf-8
__author__ = "carry" import sys
reload(sys)
sys.setdefaultencoding('utf-8') import urllib
import urllib2
import re #获取源码
def get_content(page):
headers = {#'Host':'search.51job.com',
'User-Agent':'Mozilla/5.0 (Windows NT 6.2; rv:16.0) Gecko/20100101 Firefox/16.0',
#'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
#'Connection':'keep-alive'
}
url ='http://search.51job.com/list/000000,000000,0000,00,9,99,python,2,'+ str(page)+'.html'
req = urllib2.Request(url,headers=headers)
r = urllib2.urlopen(req)
response = r.read() #读取源代码并转为unicode
html = response.decode('gbk').encode('utf-8')
return html def get(html):
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>.*? <span class="t5">(.*?)</span>',re.S)#匹配换行符
items=re.findall(reg,html)
return items #多页处理,下载到文件
for j in range(1,11):
print(u"正在爬取第"+str(j)+"页数据...")
html = get_content(j) #调用获取网页原码
for i in get(html):
#print(i[0],i[1],i[2],i[3],i[4])
with open ('51job.txt','a') as f:
f.write(i[0]+'\t'+i[1]+'\t'+i[2]+'\t'+i[3]+'\t'+i[4]+'\n')
f.write("-----------------------------------------------------")
f.close()

最新文章

  1. Mac上MySQL忘记root密码且没有权限的处理办法&amp;workbench的一些tips (转)
  2. 让tomcat支持中文cookie
  3. DAO层,Service层,Controller层、View层 的分工合作
  4. MongoDB的ObjectId和基本操作增删改查(3)
  5. Java命令
  6. mac电脑忘记账户名密码解决方法
  7. Eclipse can&#39;t install updates
  8. 理解c++11正则表达式 (1)
  9. 字符串(马拉车算法,后缀数组,稀疏表):BZOJ 3676 [Apio2014]回文串
  10. MD5加密以及验证加密-加盐
  11. 智能指针shared_ptr
  12. Struts2(二)之封装请求正文、数据类型转换、数据验证
  13. jquery-easyUI第一篇【介绍、入门、使用常用的组件】
  14. 【LintCode&#183;容易】字符串置换
  15. 翻译:JVM虚拟机规范1.7中的运行时常量池部分(一)
  16. HDU 5983(模拟魔方 模拟)
  17. CSS3实现基本图形
  18. MySQL连接、登录、密码等
  19. iOS同步后couldn&#39;t load project
  20. 【代码笔记】iOS-cell自动变化大小

热门文章

  1. (转)Java多线程之Lock的使用 (待整理)
  2. ios--&gt;制作ipa文件
  3. [STL] SET实用用法
  4. Win7 IIS建站
  5. Entity Framework Core 2.0 新特性
  6. Linux操作系统-命令-aptitude install unzip
  7. Linux工具参考篇(网摘)
  8. 设计模式之Iterator模式
  9. .Net之用户控件笔记
  10. 自定义组件-BreadcrumbTreeView 的使用