1

入门

一、使用request获取网页的源代码

URL管理器作用:抓取(已经抓取的URL集合)和(没有抓取的URL集合)

import requests  
url = 'http://tieba.baidu.com/f?kw=python&fr=ala0&tpl=5'
html = requests.get(url)   #抓取网页的源代码
print html.text

将爬虫伪造成网页进行登录:

 hea = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36'}   
html = requests.get('http://www.renren.com/327337698',headers = hea)
print html.text

二、单线程爬虫的基本原理

1)使用requests获取源代码
2)使用正则表达式匹配出管兴趣的内容,如下:

chinese = re.findall('color: #039;">(.*?)</a>',html.text,re.S)
for each in chinese:
    print each

三、向网页提交数据

  • get:从服务器获取数据
    实现方式:构造url的参数
  • post:向服务器传送数据,并获取返回值
    实现方式:将数据放在header中,提交数据

1) 表单post提交

posted @ 2017-06-27 10:54  nice_day  阅读(116)  评论(0)    收藏  举报