python学习之爬虫一

一,爬虫原理:

通过模拟浏览器的行为 自动从网上获得需要的数据

二,爬虫的流程

1,发送request请求给某个URL :

2,获得返回的response 解析 得到需要的数据 再根据自己的需要进行各种处理

三,具体的实现代码 

3.1发送request请求分2种:get 和 post  ,这里使用的是python3 使用的模块是requests ,可使用pip3 install requests(pip也行 只要你的python安装目录下的scripts文件夹里既有pip.exe 又有pip3.exe) 下载并安装

3.1.1

get请求:

get请求的参数一般有个URL就够了 

扫描二维码关注公众号,回复: 1913072 查看本文章

示例:

import requests

URL = 'https://github.com/login'
r1 = requests.get(URL)  # 用变量r1接收response
 

3.1.2

post请求:

post请求一般用于向服务端提交资料 比如登录账户 点赞 

post请求的参数比较多 一般包括url='' , headers={}, data={}, cookies={}

其中headers是要提交的请求头 一般至少包括:uesr-Agent设备信息  

data是请求体 一般至少包括: user账号 password密码  有时要有token(用于验证是否是浏览器行为 一种反爬虫手段)

cookies 一种最常见的验证方式 即反爬方式

具体的某个请求浏览器发送了哪些信息可以到浏览器的network里面找 最大限度的模拟浏览器向URL发送请求

示例:

1 r2 = requests.post(
2     url='https://github.com/session',
3     data={'login': 'xxx', 'password': 'xxx', 'authenticity_token': token},
4     cookies=r1_cookies_dict,
5     headers={'User-Agent':
6             'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'}
7                    )
View Code

3.2 获得返回的response 阶段

3.2.1

对于返回的r1以下几种操作:

# r1.content   # 原始的字节类型的内容
# r1.text   # 字符串型的内容
# r1.cookies.get_dict()  # 获得返回的cookies并将其转成dict类型
# print(r1.apparent_encoding)  # 获得返回内容的编码
# r1.encoding='utf-8'  # 将编码设置为'utf-8'
# r1.encoding=r1.apparent_encoding   # 将编码设为返回来的编码

3.2.2

用BeautifulSoup模块来提取response中的数据

下载并安装 pip install beatifulsoup4

导入模块的方式要注意 :

from bs4 import BeautifulSoup

使用模块的find和find_all方法来获得数据:

find 找到并返回第一个对象

find_all 找到所有的对象并打包成一个列表返回

示例:

import requests
from bs4 import BeautifulSoup
URL = 'https://github.com/login'
r1 = requests.get(URL)

# 注意参数为字符串形式(.text),使用了内置解析器'html.parser'也有第三方的解析器'lxml'
s1 = BeautifulSoup(r1.text, 'html.parser')
# 通过find方法找到一个name属性为特定值的input标签的value属性的值
tag_input = s1.find('input', attrs={'name': 'authenticity_token'})
token = tag_input.get('value')
# 获得返回的cookies
r1_cookies_dict = r1.cookies.get_dict()

四,一个作业的完整示例:账号密码我用xxx来代替了

'''
自动登录GitHub账号,并打印name school

writen by Zhao Shuai
qq:123456789
'''
import requests
from bs4 import BeautifulSoup

# 访问github登录页
URL = 'https://github.com/login'
r1 = requests.get(URL)
# r1.content
# r1.text
# r1.cookies.get_dict()
# print(r1.apparent_encoding)
# r1.encoding='utf-8'
# r1.encoding=r1.apparent_encoding
# print(r1.text)   # 验证 此get请求成功获得返回值


# 先找到返回过来的token
s1 = BeautifulSoup(r1.text, 'html.parser')
# tag_div = s1.find(name='div', id='login')  # 这里有个坑 不要先找这个div再找里面的hidden input 找不到 要直接找input
# print(tag_div)  # 这里能正确打印 但不是我们要的内容
tag_input = s1.find('input', attrs={'name': 'authenticity_token'})
# 到这里为止并没有获得我要的那个单独的input 而是一堆东西 不过并不影响我获得该隐藏input下的token值 不知道为什么?
# print(tag_input.get('value'))   # 成功获得token
token = tag_input.get('value')

# 获得cookies
r1_cookies_dict = r1.cookies.get_dict()
# print(r1_cookies_dict)

# 输入账户信息 带上token 登录
r2 = requests.post(
    url='https://github.com/session',
    data={'login': 'xxx', 'password': 'xxx', 'authenticity_token': token},
    cookies=r1_cookies_dict,
    headers={'User-Agent':
            'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'}
                   )
# print(r2.text)  # 验证 带上cookies 并且在data中带上token 账号 密码 正确的话 成功登录

# 进入我的个人信息页面
r3 = requests.get('https://github.com/gazs2005')
soup = BeautifulSoup(r3.text, 'html.parser')
name = soup.find('span', attrs={'itemprop': 'name'}).text
school = soup.find('span', class_='p-org').find('div').text
print('name:%s' % name)
print('school:%s' % school)
View Code

最后成功的将我的github账户的昵称(name)和工作单位(school)打印出来  当然工作单位是乱写的

猜你喜欢

转载自www.cnblogs.com/zhaoshuai5015/p/9274388.html
今日推荐