防爬蟲應(yīng)對策略:設(shè)置user-agent 使用代理IP 降低訪問頻率 驗證碼限制
網(wǎng)頁請求原理:DNS,全稱為Domain Name System,即域名系統(tǒng),是一種用于將域名和IP地址相互映射的分布式數(shù)據(jù)庫系統(tǒng)。DNS的作用就是將域網(wǎng)站轉(zhuǎn)換成相應(yīng)的服務(wù)器IP地址
?? ?HTTP協(xié)議格式: 由客戶端請求消息和服務(wù)器端相應(yīng)消息組成?
?? ?端口443:明確用于HTTPS服務(wù),因此是HTTPS(加密)流量的標準端口。它也稱為HTTPS端口443
?? ?get從服務(wù)器獲取指定頁面信息(獲取信息),post向服務(wù)器提交數(shù)據(jù)并獲取頁面信息(發(fā)送信息)
?? ?狀態(tài)碼:相應(yīng)狀態(tài)碼由三位數(shù)字組成,其中第一位數(shù)字定義了相應(yīng)的類別,有五種可能取值。
?? ??? ?100~199(服務(wù)器成功接收部分請求,要求客戶端繼續(xù)提交其余請求才能完成整個處理過程)
?? ??? ?200~299(成功接收請求并已完成整個處理過程。常為200表示OK,請求成功)
?? ??? ?300~399(為完成請求,客戶端需進一步細化請求。例如請求的資源已經(jīng)移動到一個新的地址。302'所請求頁面轉(zhuǎn)移到新的URL' 307,304'表示使用緩存資源')
?? ??? ?400~499(客戶請求有錯誤,常用狀態(tài)碼為404'服務(wù)器無法找到被請求的頁面',403'服務(wù)器拒絕訪問,權(quán)限不夠')
?? ??? ?500~599(服務(wù)器端出現(xiàn)錯誤,500'表示請求未完成,服務(wù)器遇到不可預(yù)知的情況')
url用起來麻煩 python自帶
request簡單需要pip安裝
安裝selenium:打開cmd 輸入pip install selenium (指定版本則在后面加上"==版本號") ?速度太慢找國內(nèi)pip鏡像網(wǎng)站(pip install selenium -i https://pypi.tuna.tsinghua.edu.cn/simple)
?? ??? ?查看版本:pip show selenium
?? ??? ?
user-agent表示用戶代理,是HTTP協(xié)議中的一個字段,在其請求頭部headers里面,其作用是描述發(fā)出HTTP請求的終端信息,服務(wù)器通過這個字段可以知道訪問網(wǎng)站的用戶。
超時設(shè)置 在request語句之后 file=... ... ...(url,timeout=1)無限等待為空值 ?作用:防止url不可訪問,或者響應(yīng)速度太慢而造成的時間浪費。
安裝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連接服務(wù)器 端口22tcp 用win scp
傳文件
Windows server 遠程桌面連接 端口3389
直接復(fù)制粘貼傳文件
安全組設(shè)置防火墻
防火墻概念
linux命令如cd 創(chuàng)建刪除文件夾
文本編輯器 nano
安裝Inmp全稱linuxnginxmysqlphp
ntp全稱作用給其他設(shè)備提供當(dāng)前時間
不考最難的vpn但是要會生成證書什么的代碼
傳輸層協(xié)議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 = "http://ul[@class='menu']/li[4]/a"
xpath_2 = "http://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
#不讓瀏覽器自動關(guān)閉
options = webdriver.EdgeOptions()
options.add_experimental_option('detach', True)
driver = webdriver.ChromiumEdge(options=options)
#加載網(wǎng)頁,獲取源代碼
url = 'https://www.bilibili.com/v/popular/all/'
driver.get(url)
#導(dǎo)入BeautifulSoup,篩選數(shù)據(jù)
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
? ? })
# 輸出結(jié)果
print(f"標題:{title.text},播放量:{count.text.strip()},彈幕數(shù):{dm.text.strip()}")
for comment in comments_text:
? ? print(f"評論:\nID:{comment['name']},評論內(nèi)容:{comment['text']}")文章來源:http://www.zghlxwxcb.cn/news/detail-760515.html
driver.close()文章來源地址http://www.zghlxwxcb.cn/news/detail-760515.html
到了這里,關(guān)于爬蟲和云計算考試的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!