单线程爬虫
Requests:HTTP for Humans
完美替代Python的urllib2模块
更多的自动化
更友好的用户体验
更完善的功能
第三方库安装技巧:
少用easy_install因为只能安装不能卸载
多用pip方式安装
撞墙了,请戳 http://www.lfd.uci.edu/~gohlke/pythonlibs/
第一个网页爬虫
Requests获取网页源代码
--直接获取源代码
--修改http头获取源代码
Requests与正则表达式
使用Requests获取网页源代码,再使用正则表达式匹配出感兴趣的内容,这是单线程爬虫的基本原理。
#-*-coding:utf8-*-
import requests
import re
import sys
reload(sys)
sys.setdefaultencoding("gb18030")
type=sys.getfilesystemencoding()
# headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebkit/537.36(KHTML,like Gecko)'}
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36'}
# html=requests.get('http://jp.tingroom.com/yuedu/yd300p')
html=requests.get('http://jp.tingroom.com/yuedu/yd300p',headers=headers)
html.encoding='utf-8'
# print html.text
title=re.findall('color:#666666;">(.*?)</span>',html.text,re.S)
for each in title:
print each
chinese=re.findall('color: #039;(.*?)</a>',html.text,re.S)
for each in chinese:
print each