1. 網(wǎng)頁(yè)分析
??網(wǎng)頁(yè)分析即通過(guò)檢查元素,確定想提取的內(nèi)容的區(qū)域位置,以便后面通過(guò)標(biāo)簽id,name,class或其它屬性提取內(nèi)容。網(wǎng)頁(yè)類型包括靜態(tài)網(wǎng)頁(yè)和動(dòng)態(tài)網(wǎng)頁(yè):
- 靜態(tài)網(wǎng)頁(yè)的指數(shù)據(jù)直接存儲(chǔ)在網(wǎng)頁(yè)的 html 中,不論用戶是否請(qǐng)求了數(shù)據(jù),數(shù)據(jù)就 “靜止” 在那里。
- 動(dòng)態(tài)網(wǎng)頁(yè)的數(shù)據(jù)則被 “藏” 起來(lái)了,用戶每次請(qǐng)求后,動(dòng)態(tài)網(wǎng)頁(yè)才會(huì)有一個(gè)向遠(yuǎn)程數(shù)據(jù)庫(kù)請(qǐng)求數(shù)據(jù)的“動(dòng)作”,再把數(shù)據(jù)顯示出來(lái),但用戶無(wú)法直接從網(wǎng)頁(yè)的 html 中獲取數(shù)據(jù)。
??對(duì)于靜態(tài)網(wǎng)頁(yè),瀏覽器右鍵“查看網(wǎng)頁(yè)源代碼”,這里能看到的就是GET能直接得到的HTML內(nèi)容。從源代碼看,靜態(tài)網(wǎng)頁(yè)的數(shù)據(jù)直接存在網(wǎng)頁(yè)的源代碼中,動(dòng)態(tài)網(wǎng)頁(yè)的數(shù)據(jù)不會(huì)出現(xiàn)在網(wǎng)頁(yè)源代碼中。從網(wǎng)址特征看,靜態(tài)網(wǎng)頁(yè)的數(shù)據(jù)不會(huì) “動(dòng)”,所以一個(gè)頁(yè)面就是一個(gè)網(wǎng)址,翻頁(yè)時(shí)網(wǎng)址會(huì)變化。動(dòng)態(tài)網(wǎng)頁(yè)自己會(huì) “動(dòng)”,所以哪怕請(qǐng)求新的數(shù)據(jù) ,網(wǎng)址也不會(huì)變化。比如谷歌翻譯每一次輸入新的詞匯。
網(wǎng)頁(yè)主要有以下幾種代碼格式,這些格式通常一起使用,組成了現(xiàn)代網(wǎng)頁(yè)的基本結(jié)構(gòu):
- HTML(Hypertext Markup Language):HTML是一種標(biāo)記語(yǔ)言,用于創(chuàng)建網(wǎng)頁(yè)的結(jié)構(gòu)和內(nèi)容。它使用標(biāo)簽(例如、、)來(lái)定義網(wǎng)頁(yè)的不同部分,包括文本、圖像、鏈接等。
- CSS(Cascading Style Sheets):CSS是一種樣式表語(yǔ)言,用于控制網(wǎng)頁(yè)的外觀和布局。它定義了網(wǎng)頁(yè)元素的樣式、字體、顏色、大小、位置等屬性,使網(wǎng)頁(yè)能夠呈現(xiàn)出所需的視覺(jué)效果。
- JavaScript:JavaScript是一種腳本語(yǔ)言,用于實(shí)現(xiàn)網(wǎng)頁(yè)的交互和動(dòng)態(tài)功能。通過(guò)JavaScript,可以對(duì)網(wǎng)頁(yè)進(jìn)行操作、響應(yīng)用戶的交互,以及動(dòng)態(tài)地更新網(wǎng)頁(yè)內(nèi)容。
- JSON(JavaScript Object Notation):JSON是一種輕量級(jí)的數(shù)據(jù)交換格式,常用于在客戶端和服務(wù)器之間傳輸數(shù)據(jù)。雖然它不是網(wǎng)頁(yè)的格式,但在Web開(kāi)發(fā)中經(jīng)常用于通過(guò)API或AJAX技術(shù)獲取和交換數(shù)據(jù)。
1.1 靜態(tài)網(wǎng)頁(yè)
??靜態(tài)網(wǎng)頁(yè)的內(nèi)容一般以HTML格式返回?cái)?shù)據(jù)。請(qǐng)求靜態(tài)網(wǎng)頁(yè)時(shí),服務(wù)器將返回包含HTML代碼的響應(yīng),其中包含網(wǎng)頁(yè)的各種元素,如文本、圖像、鏈接等。解析靜態(tài)網(wǎng)頁(yè)可以使用HTML解析器(如BeautifulSoup)來(lái)解析HTML代碼,并提取所需的數(shù)據(jù)或操作網(wǎng)頁(yè)的各種元素。通常,靜態(tài)網(wǎng)頁(yè)的內(nèi)容不以JSON格式返回?cái)?shù)據(jù),因?yàn)镴SON主要用于傳輸結(jié)構(gòu)化數(shù)據(jù)而不是網(wǎng)頁(yè)內(nèi)容的表示。
??需要注意的是,有些網(wǎng)站可能會(huì)在靜態(tài)網(wǎng)頁(yè)中嵌入JSON數(shù)據(jù),以便在前端JavaScript代碼中使用。在這種情況下,您可能需要查找嵌入的JSON數(shù)據(jù)并提取所需的內(nèi)容。
??通過(guò)在靜態(tài)網(wǎng)頁(yè)中嵌入JSON數(shù)據(jù),前端開(kāi)發(fā)人員可以在網(wǎng)頁(yè)加載時(shí)獲取和操作數(shù)據(jù),避免每次都向后端發(fā)送請(qǐng)求獲取數(shù)據(jù),從而提高網(wǎng)頁(yè)的性能和響應(yīng)速度。同時(shí),通過(guò)使用JSON格式,數(shù)據(jù)可以以結(jié)構(gòu)化的方式傳輸和交換,便于前端JavaScript代碼解析、提取并使用嵌入的JSON數(shù)據(jù)。
1.2 靜態(tài)網(wǎng)頁(yè)的爬取案例
import requests
from bs4 import BeautifulSoup
url = "https://lianxh.cn/news/63ffc529caf31.html"
headers = {
'User-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.67'
}
r = requests.get(url=url, headers=headers) # print(r.text)結(jié)果即靜態(tài)網(wǎng)頁(yè)源代碼
soup = BeautifulSoup(r.content, 'html.parser')
# 在這里可以使用BeautifulSoup提供的方法來(lái)提取網(wǎng)頁(yè)內(nèi)容
# 例如:soup.find('div', {'class': 'content'}) 查找具有class為'content'的<div>標(biāo)簽
# 打印網(wǎng)頁(yè)標(biāo)題
print(soup.title.string)
網(wǎng)頁(yè)https://lianxh.cn/news/63ffc529caf31.html標(biāo)題輸出結(jié)果為:
Python爬取靜態(tài)網(wǎng)站:以歷史天氣為例| 連享會(huì)主頁(yè)
1.3 動(dòng)態(tài)網(wǎng)頁(yè)
??動(dòng)態(tài)網(wǎng)頁(yè)的數(shù)據(jù)往往根據(jù)用戶請(qǐng)求進(jìn)行響應(yīng)后獲得,無(wú)法直接從網(wǎng)頁(yè)的 html 中直接獲取。點(diǎn)擊右鍵“查看網(wǎng)頁(yè)源代碼”,會(huì)發(fā)現(xiàn)有一部分網(wǎng)頁(yè)上顯示的內(nèi)容,源代碼里面沒(méi)有對(duì)應(yīng)的數(shù)據(jù),而這部分就是通過(guò)ajax異步加載出來(lái)的。就比如CSDN的評(píng)論:
![]() |
![]() |
Fig.1 評(píng)論區(qū)元素中看不到對(duì)應(yīng)的評(píng)論數(shù)據(jù) | Fig.2 但實(shí)際卻是有一條評(píng)論 |
想要爬取這部分?jǐn)?shù)據(jù),需要分析出來(lái)加載動(dòng)態(tài)網(wǎng)頁(yè)的url。
??想要爬取動(dòng)態(tài)網(wǎng)站,第一步需要利用開(kāi)發(fā)人員工具
進(jìn)行元素審查。
![]() |
Fig.3 開(kāi)發(fā)人員工具 |
![]() |
Fig.4 開(kāi)發(fā)工具常用模塊介紹 |
元素(ELements):查看網(wǎng)頁(yè)頁(yè)面的所有元素。用于查看修改元素的屬性、CSS 屬性、監(jiān)聽(tīng)事件以及斷點(diǎn)等等。
控制臺(tái)(Console):開(kāi)發(fā)人員進(jìn)行Web開(kāi)發(fā)和調(diào)試的工具。例如使用控制臺(tái)輸出調(diào)試信息、日志消息和其他自定義消息。執(zhí)行 JS 語(yǔ)句。顯示網(wǎng)頁(yè)加載過(guò)程中的網(wǎng)絡(luò)請(qǐng)求,包括請(qǐng)求的URL、響應(yīng)狀態(tài)、響應(yīng)頭、請(qǐng)求時(shí)間等信息。
源代碼(Sources):用于查看和編輯網(wǎng)頁(yè)的HTML、CSS和JavaScript代碼。
網(wǎng)絡(luò)(Network):用于監(jiān)視和分析網(wǎng)頁(yè)加載過(guò)程中的網(wǎng)絡(luò)請(qǐng)求和響應(yīng)。
在網(wǎng)絡(luò)面板上,F(xiàn)etch/XHR、JS、CSS、Img等URL篩選是用于過(guò)濾和篩選特定類型的網(wǎng)絡(luò)請(qǐng)求的選項(xiàng)。
- Fetch/XHR:用于篩選XMLHttpRequest(XHR)和Fetch API發(fā)送的網(wǎng)絡(luò)請(qǐng)求,這些請(qǐng)求通常用于通過(guò)JavaScript進(jìn)行數(shù)據(jù)交互和異步加載。
- JS:用于篩選JavaScript文件的網(wǎng)絡(luò)請(qǐng)求,包括網(wǎng)頁(yè)中引入的外部JavaScript文件、內(nèi)聯(lián)的JavaScript代碼等。
- CSS:用于篩選CSS文件的網(wǎng)絡(luò)請(qǐng)求,包括外部CSS文件和內(nèi)聯(lián)樣式。
- Img:用于篩選圖像文件(如JPEG、PNG、GIF等)的網(wǎng)絡(luò)請(qǐng)求,包括通過(guò) <img> \text{<img>} <img>標(biāo)簽加載的圖像。
開(kāi)發(fā)者工具可以用于:復(fù)制不可復(fù)制的文字、下載不能保存的圖片、編輯頁(yè)面上的任何文本、爬蟲(chóng)時(shí)審查元素等。
1.4 Google翻譯2023.7.18爬蟲(chóng)實(shí)例
PS:我知道可以利用Google Cloud Translation API進(jìn)行翻譯,但這里是為了演示爬蟲(chóng)。
1.4.1 基于網(wǎng)頁(yè)分析的Google翻譯2023.7.18爬蟲(chóng)實(shí)例
參考流程:https://blog.csdn.net/cnds123321/article/details/103321859
參考代碼:https://github.com/lushan88a/google_trans_new
- 通過(guò)post請(qǐng)求獲得的響應(yīng),分析其結(jié)構(gòu),解析翻譯結(jié)果在json數(shù)據(jù)中的位置即可。
參數(shù)部分文件google_constant.py,在主文件中需要import:
LANGUAGES = {
'af': 'afrikaans',
'sq': 'albanian',
'am': 'amharic',
'ar': 'arabic',
'hy': 'armenian',
'az': 'azerbaijani',
'eu': 'basque',
'be': 'belarusian',
'bn': 'bengali',
'bs': 'bosnian',
'bg': 'bulgarian',
'ca': 'catalan',
'ceb': 'cebuano',
'ny': 'chichewa',
'zh-cn': 'chinese (simplified)',
'zh-tw': 'chinese (traditional)',
'co': 'corsican',
'hr': 'croatian',
'cs': 'czech',
'da': 'danish',
'nl': 'dutch',
'en': 'english',
'eo': 'esperanto',
'et': 'estonian',
'tl': 'filipino',
'fi': 'finnish',
'fr': 'french',
'fy': 'frisian',
'gl': 'galician',
'ka': 'georgian',
'de': 'german',
'el': 'greek',
'gu': 'gujarati',
'ht': 'haitian creole',
'ha': 'hausa',
'haw': 'hawaiian',
'iw': 'hebrew',
'he': 'hebrew',
'hi': 'hindi',
'hmn': 'hmong',
'hu': 'hungarian',
'is': 'icelandic',
'ig': 'igbo',
'id': 'indonesian',
'ga': 'irish',
'it': 'italian',
'ja': 'japanese',
'jw': 'javanese',
'kn': 'kannada',
'kk': 'kazakh',
'km': 'khmer',
'ko': 'korean',
'ku': 'kurdish (kurmanji)',
'ky': 'kyrgyz',
'lo': 'lao',
'la': 'latin',
'lv': 'latvian',
'lt': 'lithuanian',
'lb': 'luxembourgish',
'mk': 'macedonian',
'mg': 'malagasy',
'ms': 'malay',
'ml': 'malayalam',
'mt': 'maltese',
'mi': 'maori',
'mr': 'marathi',
'mn': 'mongolian',
'my': 'myanmar (burmese)',
'ne': 'nepali',
'no': 'norwegian',
'or': 'odia',
'ps': 'pashto',
'fa': 'persian',
'pl': 'polish',
'pt': 'portuguese',
'pa': 'punjabi',
'ro': 'romanian',
'ru': 'russian',
'sm': 'samoan',
'gd': 'scots gaelic',
'sr': 'serbian',
'st': 'sesotho',
'sn': 'shona',
'sd': 'sindhi',
'si': 'sinhala',
'sk': 'slovak',
'sl': 'slovenian',
'so': 'somali',
'es': 'spanish',
'su': 'sundanese',
'sw': 'swahili',
'sv': 'swedish',
'tg': 'tajik',
'ta': 'tamil',
'tt': 'tatar',
'te': 'telugu',
'th': 'thai',
'tr': 'turkish',
'tk': 'turkmen',
'uk': 'ukrainian',
'ur': 'urdu',
'ug': 'uyghur',
'uz': 'uzbek',
'vi': 'vietnamese',
'cy': 'welsh',
'xh': 'xhosa',
'yi': 'yiddish',
'yo': 'yoruba',
'zu': 'zulu',
}
DEFAULT_SERVICE_URLS = ('translate.google.ac', 'translate.google.ad', 'translate.google.ae',
'translate.google.al', 'translate.google.am', 'translate.google.as',
'translate.google.at', 'translate.google.az', 'translate.google.ba',
'translate.google.be', 'translate.google.bf', 'translate.google.bg',
'translate.google.bi', 'translate.google.bj', 'translate.google.bs',
'translate.google.bt', 'translate.google.by', 'translate.google.ca',
'translate.google.cat', 'translate.google.cc', 'translate.google.cd',
'translate.google.cf', 'translate.google.cg', 'translate.google.ch',
'translate.google.ci', 'translate.google.cl', 'translate.google.cm',
'translate.google.cn', 'translate.google.co.ao', 'translate.google.co.bw',
'translate.google.co.ck', 'translate.google.co.cr', 'translate.google.co.id',
'translate.google.co.il', 'translate.google.co.in', 'translate.google.co.jp',
'translate.google.co.ke', 'translate.google.co.kr', 'translate.google.co.ls',
'translate.google.co.ma', 'translate.google.co.mz', 'translate.google.co.nz',
'translate.google.co.th', 'translate.google.co.tz', 'translate.google.co.ug',
'translate.google.co.uk', 'translate.google.co.uz', 'translate.google.co.ve',
'translate.google.co.vi', 'translate.google.co.za', 'translate.google.co.zm',
'translate.google.co.zw', 'translate.google.co', 'translate.google.com.af',
'translate.google.com.ag', 'translate.google.com.ai', 'translate.google.com.ar',
'translate.google.com.au', 'translate.google.com.bd', 'translate.google.com.bh',
'translate.google.com.bn', 'translate.google.com.bo', 'translate.google.com.br',
'translate.google.com.bz', 'translate.google.com.co', 'translate.google.com.cu',
'translate.google.com.cy', 'translate.google.com.do', 'translate.google.com.ec',
'translate.google.com.eg', 'translate.google.com.et', 'translate.google.com.fj',
'translate.google.com.gh', 'translate.google.com.gi', 'translate.google.com.gt',
'translate.google.com.hk', 'translate.google.com.jm', 'translate.google.com.kh',
'translate.google.com.kw', 'translate.google.com.lb', 'translate.google.com.lc',
'translate.google.com.ly', 'translate.google.com.mm', 'translate.google.com.mt',
'translate.google.com.mx', 'translate.google.com.my', 'translate.google.com.na',
'translate.google.com.ng', 'translate.google.com.ni', 'translate.google.com.np',
'translate.google.com.om', 'translate.google.com.pa', 'translate.google.com.pe',
'translate.google.com.pg', 'translate.google.com.ph', 'translate.google.com.pk',
'translate.google.com.pr', 'translate.google.com.py', 'translate.google.com.qa',
'translate.google.com.sa', 'translate.google.com.sb', 'translate.google.com.sg',
'translate.google.com.sl', 'translate.google.com.sv', 'translate.google.com.tj',
'translate.google.com.tr', 'translate.google.com.tw', 'translate.google.com.ua',
'translate.google.com.uy', 'translate.google.com.vc', 'translate.google.com.vn',
'translate.google.com', 'translate.google.cv', 'translate.google.cx',
'translate.google.cz', 'translate.google.de', 'translate.google.dj',
'translate.google.dk', 'translate.google.dm', 'translate.google.dz',
'translate.google.ee', 'translate.google.es', 'translate.google.eu',
'translate.google.fi', 'translate.google.fm', 'translate.google.fr',
'translate.google.ga', 'translate.google.ge', 'translate.google.gf',
'translate.google.gg', 'translate.google.gl', 'translate.google.gm',
'translate.google.gp', 'translate.google.gr', 'translate.google.gy',
'translate.google.hn', 'translate.google.hr', 'translate.google.ht',
'translate.google.hu', 'translate.google.ie', 'translate.google.im',
'translate.google.io', 'translate.google.iq', 'translate.google.is',
'translate.google.it', 'translate.google.je', 'translate.google.jo',
'translate.google.kg', 'translate.google.ki', 'translate.google.kz',
'translate.google.la', 'translate.google.li', 'translate.google.lk',
'translate.google.lt', 'translate.google.lu', 'translate.google.lv',
'translate.google.md', 'translate.google.me', 'translate.google.mg',
'translate.google.mk', 'translate.google.ml', 'translate.google.mn',
'translate.google.ms', 'translate.google.mu', 'translate.google.mv',
'translate.google.mw', 'translate.google.ne', 'translate.google.nf',
'translate.google.nl', 'translate.google.no', 'translate.google.nr',
'translate.google.nu', 'translate.google.pl', 'translate.google.pn',
'translate.google.ps', 'translate.google.pt', 'translate.google.ro',
'translate.google.rs', 'translate.google.ru', 'translate.google.rw',
'translate.google.sc', 'translate.google.se', 'translate.google.sh',
'translate.google.si', 'translate.google.sk', 'translate.google.sm',
'translate.google.sn', 'translate.google.so', 'translate.google.sr',
'translate.google.st', 'translate.google.td', 'translate.google.tg',
'translate.google.tk', 'translate.google.tl', 'translate.google.tm',
'translate.google.tn', 'translate.google.to', 'translate.google.tt',
'translate.google.us', 'translate.google.vg', 'translate.google.vu', 'translate.google.ws')
主文件:
EMMM,這部分代碼說(shuō)檢測(cè)到個(gè)人信息無(wú)法發(fā)布
if __name__ == "__main__":
translator = google_translator()
translate_text = translator.translate('?????????', lang_tgt='en')
detect_result = translator.detect('?????????')
print(detect_result, translate_text)
運(yùn)行結(jié)果:
1.4.2 基于Selenium的Google翻譯2023.7.18爬蟲(chóng)實(shí)例
??簡(jiǎn)單高效的抓取動(dòng)態(tài)網(wǎng)頁(yè)內(nèi)容用動(dòng)態(tài)網(wǎng)頁(yè)抓取神器:Selenium
比如,常見(jiàn)反爬手段可粗略分為五大類:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-595477.html
- headers 字段:User-Agent、referer、cookie
- IP 地址
- js:js 實(shí)現(xiàn)跳轉(zhuǎn)、js 生成請(qǐng)求參數(shù)或數(shù)據(jù)加密
- 驗(yàn)證碼
- 其他:自定義字體(比如:貓眼電影)、CSS像素偏移(比如:去哪兒網(wǎng))
而對(duì)于用戶來(lái)說(shuō),既然你有「盾」護(hù),那就只能以鋒「矛」應(yīng)對(duì)了。反反爬的主要思路是盡可能地模擬瀏覽器,瀏覽器如何操作,代碼中就如何實(shí)現(xiàn)。所以反爬與反反爬其實(shí)就處于「動(dòng)態(tài)博弈」之中。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-595477.html
到了這里,關(guān)于自動(dòng)化測(cè)試(一):網(wǎng)頁(yè)結(jié)構(gòu)分析與Google翻譯2023.7.18爬蟲(chóng)實(shí)例的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!