Datawhale 爬虫 task 03

本文内容源自datawhale 爬虫组队学习的教程,更多知识可参考:datawhale爬虫

关于IP

为什么会出现IP被封
网站为了防止被爬取,会有反爬机制,对于同一个IP地址的大量同类型的访问,会封锁IP,过一段时间后,才能继续访问

如何应对IP被封的问题
有几种套路:

  1. 修改请求头,模拟浏览器(而不是代码去直接访问)去访问
  2. 采用代理IP并轮换
  3. 设置访问时间间隔
  4. 如何获取代理IP地址

如何获取代理IP地址
从该网站获取: https://www.xicidaili.com/
inspect -> 鼠标定位:
要获取的代理IP地址,属于class = "odd"标签的内容:代码如下,获取的代理IP保存在proxy_ip_list列表中

# 案例代码
from bs4 import BeautifulSoup
import requests
import time

def open_proxy_url(url):
    user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'
    headers = {'User-Agent': user_agent}
    try:
        r = requests.get(url, headers = headers, timeout = 20)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return(r.text)
    except:
        print('无法访问网页' + url)


def get_proxy_ip(response):
    proxy_ip_list = []
    soup = BeautifulSoup(response, 'html.parser')
    proxy_ips = soup.find(id = 'ip_list').find_all('tr')
    for proxy_ip in proxy_ips:
        if len(proxy_ip.select('td')) >=8:
            ip = proxy_ip.select('td')[1].text
            port = proxy_ip.select('td')[2].text
            protocol = proxy_ip.select('td')[5].text
            if protocol in ('HTTP','HTTPS','http','https'):
                proxy_ip_list.append(f'{protocol}://{ip}:{port}')
    return proxy_ip_list

if __name__ == '__main__':
    proxy_url = 'https://www.xicidaili.com/'
    text = open_proxy_url(proxy_url)
    proxy_ip_filename = 'proxy_ip.txt'
    with open(proxy_ip_filename, 'w') as f:
        f.write(text)
    text = open(proxy_ip_filename, 'r').read()
    proxy_ip_list = get_proxy_ip(text)
    print(proxy_ip_list)

使用代理

  • proxies的格式是一个字典:
  • {‘http’: ‘http://IP:port‘,‘https’:'https://IP:port‘}
  • 把它直接传入requests的get方法中即可
  • web_data = requests.get(url, headers=headers, proxies=proxies)

确认代理IP地址有效性
无论是免费还是收费的代理网站,提供的代理IP都未必有效,我们应该验证一下,有效后,再放入我们的代理IP池中,以下通过几种方式:访问网站,得到的返回码是200真正的访问某些网站,获取title等,验证title与预计的相同访问某些可以提供被访问IP的网站,类似于“查询我的IP”的网站,查看返回的IP地址是什么验证返回码

def check_proxy_avaliability(proxy):
    url = 'http://www.baidu.com'
    result = open_url_using_proxy(url, proxy)
    VALID_PROXY = False
    if result:
        text, status_code = result
        if status_code == 200:
            print('有效代理IP: ' + proxy)
        else:
            print('无效代理IP: ' + proxy)

完整代码

from bs4 import BeautifulSoup
import requests
import re
import json


def open_proxy_url(url):
    user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'
    headers = {'User-Agent': user_agent}
    try:
        r = requests.get(url, headers = headers, timeout = 10)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except:
        print('无法访问网页' + url)


def get_proxy_ip(response):
    proxy_ip_list = []
    soup = BeautifulSoup(response, 'html.parser')
    proxy_ips = soup.find(id = 'ip_list').find_all('tr')
    for proxy_ip in proxy_ips:
        if len(proxy_ip.select('td')) >=8:
            ip = proxy_ip.select('td')[1].text
            port = proxy_ip.select('td')[2].text
            protocol = proxy_ip.select('td')[5].text
            if protocol in ('HTTP','HTTPS','http','https'):
                proxy_ip_list.append(f'{protocol}://{ip}:{port}')
    return proxy_ip_list


def open_url_using_proxy(url, proxy):
    user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'
    headers = {'User-Agent': user_agent}
    proxies = {}
    if proxy.startswith(('HTTPS','https')):
        proxies['https'] = proxy
    else:
        proxies['http'] = proxy

    try:
        r = requests.get(url, headers = headers, proxies = proxies, timeout = 10)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return (r.text, r.status_code)
    except:
        print('无法访问网页' + url)
        print('无效代理IP: ' + proxy)
        return False


def check_proxy_avaliability(proxy):
    url = 'http://www.baidu.com'
    result = open_url_using_proxy(url, proxy)
    VALID_PROXY = False
    if result:
        text, status_code = result
        if status_code == 200:
            r_title = re.findall('<title>.*</title>', text)
            if r_title:
                if r_title[0] == '<title>百度一下,你就知道</title>':
                    VALID_PROXY = True
        if VALID_PROXY:
            check_ip_url = 'https://jsonip.com/'
            try:
                text, status_code = open_url_using_proxy(check_ip_url, proxy)
            except:
                return

            print('有效代理IP: ' + proxy)
            with open('valid_proxy_ip.txt','a') as f:
                f.writelines(proxy)
            try:
                source_ip = json.loads(text).get('ip')
                print(f'源IP地址为:{source_ip}')
                print('='*40)
            except:
                print('返回的非json,无法解析')
                print(text)
    else:
        print('无效代理IP: ' + proxy)


if __name__ == '__main__':
    proxy_url = 'https://www.xicidaili.com/'
    proxy_ip_filename = 'proxy_ip.txt'
    text = open(proxy_ip_filename, 'r').read()
    proxy_ip_list = get_proxy_ip(text)
    for proxy in proxy_ip_list:
        check_proxy_avaliability(proxy)

关于http和https代理
可以看到proxies中有两个键值对:
{‘http’: ‘http://IP:port‘,‘https’:'https://IP:port‘}
其中 HTTP 代理,只代理 HTTP 网站,对于 HTTPS 的网站不起作用,也就是说,用的是本机 IP,反之亦然。
我刚才使用的验证的网站是https://jsonip.com, 是HTTPS网站所以探测到的有效代理中,如果是https代理,则返回的是代理地址;如果是http代理,将使用本机IP进行访问,返回的是我的公网IP地址

selenium
selenium是什么:一个自动化测试工具(大家都是这么说的)

selenium应用场景:用代码的方式去模拟浏览器操作过程(如:打开浏览器、在输入框里输入文字、回车等),在爬虫方面很有必要

准备工作:
1.安装selenium(pip install selenium)
2.安装chromedriver(一个驱动程序,用以启动chrome浏览器,具体的驱动程序需要对应的驱动,在官网上可以找到下载地址) 基本步骤:
1、导入模块:

from selenium import webdriver  # 启动浏览器需要用到
from selenium.webdriver.common.keys import Keys  # 提供键盘按键支持(最后一个K要大写)

2、创建一个WebDriver实例:

driver = webdriver.Chrome("chromedriver驱动程序路径")

3、打开一个页面:

driver.get("http://www.python.org")  # 这个时候chromedriver会打开一个Chrome浏览器窗口,显示的是网址所对应的页面

4、关闭页面

driver.close()  # 关闭浏览器一个Tab
# or
driver.quit()  # 关闭浏览器窗口

高级-查找元素:
在打开页面和关闭页面中间,就是各种操作!而查找元素这一点,和爬虫常见的HTML页面解析,定位到具体的某个元素基本一样,只不过,调用者是driver

element = driver.find_element_by_name("q")  

高级-页面交互:
找到元素后,就是进行“交互”,如键盘输入(需提前导入模块)


element.send_keys(“some text”)  # 往一个可以输入对象中输入“some text”
#甚至

element.send_keys(Keys.RETURN)  # 模拟键盘回车
#一般来说,这种方式输入后会一直存在,而要清空某个文本框中的文字,就需要:

element.clear()  # 清空element对象中的文字

高级-等待页面加载(wait)
应用场景:含有ajax加载的page!因为在这种情况下,页面内的某个节点并不是在一开始就出现了,而在这种情况下,就不能“查找元素”,元素选择不到,就不好进行交互操作!等待页面加载这两个模块经常是一起导入的:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

显示等待:触发某个条件后才能够执行后续的代码

river = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:    
    element = WebDriverWait(driver, 10).until(           
        EC.presence_of_element_located((By.ID, "myDynamicElement")))
finally:    
    driver.quit()
#其中,presence_of_element_located是条件,By.ID是通过什么方式来确认元素(这个是通过id),"myDynamicElement"这个就是某个元素的ID

隐示等待:设置某个具体的等待时间

driver = webdriver.Firefox()
driver.implicitly_wait(10) # seconds
driver.get("http://somedomain/url_that_delays_loading")
myDynamicElement = driver.find_element_by_id("myDynami")

session和cookie
Session 是会话的意思,会话是产生在服务端的,用来保存当前用户的会话信息,而 Cookies 是保存在客户端(浏览器),有了 Cookie 以后,客户端(浏览器)再次访问服务端的时候,会将这个 Cookie 带上,这时,服务端可以通过 Cookie 来识别本次请求到底是谁在访问。

可以简单理解为 Cookies 中保存了登录凭证,我们只要持有这个凭证,就可以在服务端保持一个登录状态。

在爬虫中,有时候遇到需要登录才能访问的网页,只需要在登录后获取了 Cookies ,在下次访问的时候将登录后获取到的 Cookies 放在请求头中,这时,服务端就会认为我们的爬虫是一个正常登录用户。

session
那么,Cookies 是如何保持会话状态的呢?

在客户端(浏览器)第一次请求服务端的时候,服务端会返回一个请求头中带有 Set-Cookie 字段的响应给客户端(浏览器),用来标记是哪一个用户,客户端(浏览器)会把这个 Cookies 给保存起来。
当我们输入好用户名和密码时,客户端会将这个 Cookies 放在请求头一起发送给服务端,这时,服务端就知道是谁在进行登录操作,并且可以判断这个人输入的用户名和密码对不对,如果输入正确,则在服务端的 Session 记录一下这个人已经登录成功了,下次再请求的时候这个人就是登录状态了。

如果客户端传给服务端的 Cookies 是无效的,或者这个 Cookies 根本不是由这个服务端下发的,或者这个 Cookies 已经过期了,那么接下里的请求将不再能访问需要登录后才能访问的页面。

所以, Session 和 Cookies 之间是需要相互配合的,一个在服务端,一个在客户端。

cookies
具体操作方式还是在 Chrome 中按 F12 打开开发者工具,选择 Application 标签,点开 Cookies 这一栏。

Name:这个是 Cookie 的名字。一旦创建,该名称便不可更改。
Value:这个是 Cookie 的值。
Domain:这个是可以访问该 Cookie 的域名。例如,如果设置为 .jd.com ,则所有以 jd.com ,结尾的域名都可以访问该Cookie。
Max Age:Cookie 失效的时间,单位为秒,也常和 Expires 一起使用。 Max Age 如果为正数,则在 Max Age 秒之后失效,如果为负数,则关闭浏览器时 Cookie 即失效,浏览器也不会保存该 Cookie 。
Path:Cookie 的使用路径。如果设置为 /path/ ,则只有路径为 /path/ 的页面可以访问该 Cookie 。如果设置为 / ,则本域名下的所有页面都可以访问该 Cookie 。
Size:Cookie 的大小。
HTTPOnly:如果此项打勾,那么通过 JS 脚本将无法读取到 Cookie 信息,这样能有效的防止 XSS 攻击,窃取 Cookie 内容,可以增加 Cookie 的安全性。
Secure:如果此项打勾,那么这个 Cookie 只能用 HTTPS 协议发送给服务器,用 HTTP 协议是不发送的。
那么有的网站为什么这次关闭了,下次打开的时候还是登录状态呢?

这就要说到 Cookie 的持久化了,其实也不能说是持久化,就是 Cookie 失效的时间设置的长一点,比如直接设置到 2099 年失效,这样,在浏览器关闭后,这个 Cookie 是会保存在我们的硬盘中的,下次打开浏览器,会再从我们的硬盘中将这个 Cookie 读取出来,用来维持用户的会话状态。

第二个问题产生了,服务端的会话也会无限的维持下去么,当然不会,这就要在 Cookie 和 Session 上做文章了, Cookie 中可以使用加密的方式将用户名记录下来,在下次将 Cookies 读取出来由请求发送到服务端后,服务端悄悄的自己创建一个用户已经登录的会话,这样我们在客户端看起来就好像这个登录会话是一直保持的。

一个重要概念
当我们关闭浏览器的时候会自动销毁服务端的会话,这个是错误的,因为在关闭浏览器的时候,浏览器并不会额外的通知服务端说,我要关闭了,你把和我的会话销毁掉吧。

因为服务端的会话是保存在内存中的,虽然一个会话不会很大,但是架不住会话多啊,硬件毕竟是会有限制的,不能无限扩充下去的,所以在服务端设置会话的过期时间就非常有必要。

当然,有没有方式能让浏览器在关闭的时候同步的关闭服务端的会话,当然是可以的,我们可以通过脚本语言 JS 来监听浏览器关闭的动作,当浏览器触发关闭动作的时候,由 JS 像服务端发起一个请求来通知服务端销毁会话。

由于不同的浏览器对 JS 事件的实现机制不一致,不一定保证 JS 能监听到浏览器关闭的动作,所以现在常用的方式还是在服务端自己设置会话的过期时间

实战案例:模拟登录163
代码实现:

import time

from selenium import webdriver
from selenium.webdriver.common.by import By


"""
使用selenium进行模拟登陆
1.初始化ChromDriver
2.打开163登陆页面
3.找到用户名的输入框,输入用户名
4.找到密码框,输入密码
5.提交用户信息
"""
name = '*'
passwd = '*'
driver = webdriver.Chrome('./chromedriver')
driver.get('https://mail.163.com/')
# 将窗口调整最大
driver.maximize_window()
# 休息5s
time.sleep(5)
current_window_1 = driver.current_window_handle
print(current_window_1)

button = driver.find_element_by_id('lbNormal')
button.click()

driver.switch_to.frame(driver.find_element_by_xpath("//iframe[starts-with(@id, 'x-URS-iframe')]"))

email = driver.find_element_by_name('email')
#email = driver.find_element_by_xpath('//input[@name="email"]')
email.send_keys(name)
password = driver.find_element_by_name('password')
#password = driver.find_element_by_xpath("//input[@name='password']")
password.send_keys(passwd)
submit = driver.find_element_by_id("dologin")
time.sleep(15)
submit.click()
time.sleep(10)
print(driver.page_source)
driver.quit()

作业
Q1: 怎么在ip被封之后实现自动更换代理池内的代理?

A1: 用random.choice 随机选取ip

Q2: 如何用一句通俗的语言解释清楚request、beautifulsoup和selenium三者与浏览器之间的关系?

A2: BeautifulSoup:处理速度快,同时可以连续查找,主要用于静态网页

经过BeautifulSoup处理以后,编码方式都变成了Unicode,需要将其变成所需的编码方式:可以利用encode(‘需要的编码’),还可以利用 BeautifulSoup(网页/html, lxml/xml”).prettify(‘需要的编码’) 可以利用soup.original_encoding检测原来的编码。 Selenium:主要用于动态网页,查找速度慢,解析时要注意 .find_elements_by_xpath和.find_element_by_xpath有区别,同时利用浏览器时要配置。 .PhantomJS: drive=webdriver.PhantomJS(‘D:\Anaconda2\phantomjswindows\binphantomjs.exe’)

Q3: 构建好代理池后,如何在一次爬虫中自动切换代理? 比如代理无效,或者代理ip被封,这时自动切换下一个ip。

A3: 首先你要有一个ip代理池(如果比较豪可以自己买真实ip自行搭建,好处独享有技术门槛,或者找第三方ip代理商对接,好吃廉价,但ip不独享), 真实ip需要自己写程序来代理转发,第三方ip代理商则会提供相关转发API,直接调用就可以,这东西没什么技术难度

Q4: ip_list.append(f’{protpcol}😕/{ip}:{port}’)这里的f是格式化?

A4:

  • 从代理ip网站爬取IP地址及端口号并储存
  • 验证ip是否能用
  • 格式化ip地址
  • 在requests中使用代理ip爬取网站
    小项目
    挑战项目:模拟登录丁香园,并抓取论坛页面所有的人员基本信息与回复帖子内容。
    丁香园论坛:http://www.dxy.cn/bbs/thread/626626#626626 。
    代码来源
# 丁香园论坛:http://www.dxy.cn/bbs/thread/626626#626626 。
# 丁香园用户名:xxxx
# 密码:ABcd1234

from selenium import webdriver
import time
from lxml import etree
class DingxiangyuanLogin():
    def run(self):
        # 1.请求头:
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"}
        #2. 添加请求头信息
        options = webdriver.ChromeOptions()
        options.add_argument('user-agent="ozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"')
        #3. 创建浏览器驱动
        chrome_driver = webdriver.Chrome(options=options)

        #4. 登录账号
        #4.1 访问登录界面
        url = "https://auth.dxy.cn/accounts/login?"
        chrome_driver.get(url=url)
        time.sleep(3)
        #4.2 定位电脑登录按钮:
        pc_login_selec_button = chrome_driver.find_element_by_class_name('login__tab_wp').find_elements_by_tag_name('a')[1]
        #4.3 点击进入电脑登录界面:
        pc_login_selec_button.click()
        time.sleep(3)
        #4.4 定位“用户名”输入框
        user_name_box = chrome_driver.find_element_by_name('username')
        #4.5 输入用户名:
        user_name_box.send_keys('xxxx')
        #4.6 定位“密码”输入框
        code_box = chrome_driver.find_element_by_name('password')
        #4.7 输入密码
        code_box.send_keys('ABcd1234')
        #4.8 定位登录按钮
        login_button = chrome_driver.find_element_by_class_name('form__button')
        #4.9 点击登录按钮
        login_button.click()

        #5. 访问目标帖子界面
        chrome_driver.get('http://www.dxy.cn/bbs/thread/626626#626626 ')

        #获取帖子网页源代码
        reply_list = []
        response_data = chrome_driver.page_source
        #使用Xpath解析内容
        xpath_data = etree.HTML(response_data)
        # 获取所有回复节点
        # starts-with(@title,"注册时间")
        replies = xpath_data.xpath('//div[starts-with(@id, "post_")]')
        print("replies: " + str(replies))
        # print(replies)
        for reply in replies:
            reply_dict = {}
            print('reply: ' + str(reply))
            # 回复人姓名:
            auth_name = reply.xpath('.//div[@class="auth"]')[0].xpath('string(.)')
            # print('auth_name: ' + str(auth_name))
            # 级别
            auth_rank = reply.xpath('.//div[@class="info clearfix"]')[0].xpath('string(.)').strip()
            print("auth_rank: " + str(auth_rank))
            # 回复内容
            reply_content = reply.xpath('.//td[@class="postbody"]')[0].xpath('string(.)').strip()
            print('reply_content: ' + str(reply_content))
            reply_dict['auth_name'] = auth_name
            reply_dict['auth_rank'] = auth_rank
            reply_dict['reply_content'] = reply_content
            reply_list.append(reply_dict)

DingxiangyuanLogin().run()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值