爬虫和云计算考试

防爬虫应对策略:设置user-agent 使用代理IP 降低访问频率 验证码限制
网页请求原理:DNS,全称为Domain Name System,即域名系统,是一种用于将域名和IP地址相互映射的分布式数据库系统。DNS的作用就是将域网站转换成相应的服务器IP地址
    HTTP协议格式: 由客户端请求消息和服务器端相应消息组成 
    端口443:明确用于HTTPS服务,因此是HTTPS(加密)流量的标准端口。它也称为HTTPS端口443
    get从服务器获取指定页面信息(获取信息),post向服务器提交数据并获取页面信息(发送信息)
    状态码:相应状态码由三位数字组成,其中第一位数字定义了相应的类别,有五种可能取值。
        100~199(服务器成功接收部分请求,要求客户端继续提交其余请求才能完成整个处理过程)
        200~299(成功接收请求并已完成整个处理过程。常为200表示OK,请求成功)
        300~399(为完成请求,客户端需进一步细化请求。例如请求的资源已经移动到一个新的地址。302'所请求页面转移到新的URL' 307,304'表示使用缓存资源')
        400~499(客户请求有错误,常用状态码为404'服务器无法找到被请求的页面',403'服务器拒绝访问,权限不够')
        500~599(服务器端出现错误,500'表示请求未完成,服务器遇到不可预知的情况')
url用起来麻烦 python自带
request简单需要pip安装
安装selenium:打开cmd 输入pip install selenium (指定版本则在后面加上"==版本号")  速度太慢找国内pip镜像网站(pip install selenium -i https://pypi.tuna.tsinghua.edu.cn/simple)
        查看版本:pip show selenium
        
user-agent表示用户代理,是HTTP协议中的一个字段,在其请求头部headers里面,其作用是描述发出HTTP请求的终端信息,服务器通过这个字段可以知道访问网站的用户。
超时设置 在request语句之后 file=... ... ...(url,timeout=1)无限等待为空值  作用:防止url不可访问,或者响应速度太慢而造成的时间浪费。
安装beautifulsoup:pip install beautifulsoup4 
安装lxml:一:pip install lxml 报错方法二:先安装wheel库  pip install wheel 查看python版本 然后从pypi.python.org上下载lxml的.whl文件 找到文件位置打开cmd 输入pip install+文件全名
xpath:书本p66

分布式
会做实验就没问题
Linux ssh连接服务器 端口22tcp 用win scp
传文件
Windows server 远程桌面连接 端口3389
直接复制粘贴传文件
安全组设置防火墙
防火墙概念
linux命令如cd 创建删除文件夹
文本编辑器 nano
安装Inmp全称linuxnginxmysqlphp
ntp全称作用给其他设备提供当前时间
不考最难的vpn但是要会生成证书什么的代码
传输层协议TCP UDP 端口80 43
上机一道linux操作题一道实验题之一
没有最难的vpn

(1)

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver import ActionChains

import time

options = webdriver.ChromeOptions()

options.add_experimental_option('detach', True)

driver = webdriver.Chrome(options=options)

driver.get('https://yjsy.hunnu.edu.cn')

time.sleep(5)

xpath_1 = "//ul[@class='menu']/li[4]/a"

xpath_2 = "//ul[@class='menu']/li[4]/ul/li[2]/a"

button_1 = driver.find_element(By.XPATH, xpath_1)

button_2 = driver.find_element(By.XPATH, xpath_2)

ActionChains(driver).move_to_element(button_1).perform()

time.sleep(5)

ActionChains(driver).move_to_element(button_2).click().perform()

(2)

from selenium import webdriver

from selenium.webdriver.common.by import By

#不让浏览器自动关闭

options = webdriver.EdgeOptions()

options.add_experimental_option('detach', True)

driver = webdriver.ChromiumEdge(options=options)

#加载网页,获取源代码

url = 'https://www.bilibili.com/v/popular/all/'

driver.get(url)

#导入BeautifulSoup,筛选数据

from bs4 import BeautifulSoup

soup = BeautifulSoup(driver.page_source, 'lxml')

result = soup.find_all('div', class_='video-card')

for item in result:

    title = item.find('p', class_='video-name')

    up = item.find('span', class_='up-name__text')

    count = item.find('span', class_='play-text')

    print(f'视频:{title.text},UP:{up.text},播放量:{count.text.strip()}')

(3)

from selenium import webdriver

url = 'https://www.bilibili.com/video/BV1iN4y1a7KJ'

options = webdriver.ChromeOptions()

options.add_experimental_option('detach', True)

driver = webdriver.Chrome(options=options)

driver.get(url)

import time

time.sleep(5)

html = driver.page_source

from bs4 import BeautifulSoup

soup = BeautifulSoup(html, 'lxml')

title = soup.find('h1', class_="video-title")

count = soup.find('span', class_="view item")

dm = soup.find('span', class_="dm item")

datetime = soup.find('span', class_="pubdate-text")

comments = soup.find_all('div', class_="content-warp")

comments_text = []

for comment in comments:

    name = comment.find('div', class_="user-info").text

    text = comment.find('span', class_="reply-content").text

    comments_text.append({

        'name': name,

        'text': text

    })

# 输出结果

print(f"标题:{title.text},播放量:{count.text.strip()},弹幕数:{dm.text.strip()}")

for comment in comments_text:

    print(f"评论:\nID:{comment['name']},评论内容:{comment['text']}")

driver.close()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值