码迷,mamicode.com
首页 > 编程语言 > 详细

python爬虫(一)

时间:2016-07-08 01:30:55      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

使用环境window10+python3.4

先安装requests

python3 -m pip install requests

 

1.  先使用青年文摘网站看看效果

import requests
html = requests.get("http://www.qnwz.cn/index.html")
print(html.content)

如果我们想把当前文本保存在一个文件里,可以这样操作

with open(filename, wb) as fd:
    for c in html.iter_content():
        fd.write(c)

2. 有的时候网页就不可以直接爬取了,这时候可能要提交表单

import requests
params = {‘xxx: ‘xxx, ‘xxx: ‘xxx}
r = requests.post("http://who_am_i.com/form.php", data=params) #注意这里使用post方法来提交表单
print(r.text)

可能还会让你提交文件或者图像

import requests
files = {‘f: open(‘1.png, rb)}
r = requests.post("http://who_am_i.com/xixi.php",files=files)
print(r.text)  #看起来也不会太复杂

也许还需要你处理登陆和cookies

import requests
session = requests.Session()
params = {username: username, password: password}
s = session.post("http://who_am_i.com/haha.php", params)print(s.cookies.get_dict())
s = session.get("http://who_am_i.com/a.php")
print(s.text)

也许有时候会弹出一个登陆窗口,这时候requests还是能够优雅的处理

import requests
from requests.auth import AuthBase
from requests.auth import HTTPBasicAuth
auth = HTTPBasicAuth(‘username, password)
r = requests.post(url="http://who_am_i.com//login.php", auth=auth)
print(r.text)

同时还有登陆需要验证码问题,这个就不太好处理了,一般思路是编写代码获取验证码的图片,手动输入,或者通过工具对验证码进行识别,自动输入,比如python的pytesseract就有识别验证码的功能,不妨一试。

3. 有些时候网页使用JavaScript渲染的,这时候通过requests直接获取的页面并不能像浏览器所看到的那样,这时候不妨下载一个PhantomJS程序来渲染js,要想在python中使用,需要安装selenium , 这个包具有模拟浏览器的功能

from selenium import webdriver
import time
driver = webdriver.PhantomJS(executable_path=‘填入PhantomJS程序安装路径,如D:\p\bin\PhantomJs‘)
driver.get("http://weixin.sogou.com/weixin?type=1&query=dp")
time.sleep(3)
print(driver.find_element_by_id("content").text)
driver.close()

基本步骤就这样,深入了解就查看selenium文档

 

 

 

 

python爬虫(一)

标签:

原文地址:http://www.cnblogs.com/who-a/p/5652017.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!