Python爬蟲實(shí)現(xiàn)(requests、BeautifulSoup和selenium)
requests實(shí)現(xiàn)
Python requests 是一個(gè)常用的 HTTP 請(qǐng)求庫,可以方便地向網(wǎng)站發(fā)送 HTTP 請(qǐng)求,并獲取響應(yīng)結(jié)果。
下載requests庫
pip install requests
實(shí)例:
# 導(dǎo)入 requests 包
import requests
# 發(fā)送請(qǐng)求
x = requests.get('https://www.runoob.com/')
# 返回網(wǎng)頁內(nèi)容
print(x.text)
屬性和方法
屬性或方法 | 說明 |
---|---|
content | 返回響應(yīng)的內(nèi)容,以字節(jié)為單位 |
headers | 返回響應(yīng)頭,字典格式 |
json() | 返回結(jié)果的 JSON 對(duì)象 |
request | 返回請(qǐng)求此響應(yīng)的請(qǐng)求對(duì)象 |
status_code | 返回 http 的狀態(tài)碼 |
text | 返回響應(yīng)的內(nèi)容,unicode 類型數(shù)據(jù) |
url | 返回響應(yīng)的 URL |
附加請(qǐng)求參數(shù)
發(fā)送請(qǐng)求我們可以在請(qǐng)求中附加額外的參數(shù),例如請(qǐng)求頭、查詢參數(shù)、請(qǐng)求體等,例如:
headers = {'User-Agent': 'Mozilla/5.0'} # 設(shè)置請(qǐng)求頭
params = {'key1': 'value1', 'key2': 'value2'} # 設(shè)置查詢參數(shù)
data = {'username': 'example', 'password': '123456'} # 設(shè)置請(qǐng)求體
response = requests.post('https://www.runoob.com', headers=headers, params=params, data=data)
上述代碼發(fā)送一個(gè) POST 請(qǐng)求,并附加了請(qǐng)求頭、查詢參數(shù)和請(qǐng)求體。
除了基本的 GET 和 POST 請(qǐng)求外,requests 還支持其他 HTTP 方法,如 PUT、DELETE、HEAD、OPTIONS 等。
使用 Beautiful Soup 解析 html 文件
Beautiful Soup is a library that makes it easy to scrape information from web pages. It sits atop an HTML or XML parser, providing Pythonic idioms for iterating, searching, and modifying the parse tree.
Beautiful Soup 是一個(gè)可以輕松從網(wǎng)頁中抓取信息的庫。它位于 HTML 或 XML 解析器之上,提供用于迭代、搜索和修改解析樹的 Pythonic 習(xí)慣用法。
beautifulsoup包安裝
pip install beautifulsoup4
實(shí)例:
from bs4 import BeautifulSoup
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a class="sister" id="link1">Elsie</a>,
<a class="sister" id="link2">Lacie</a> and
<a class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
# 創(chuàng)建一個(gè)BeautifulSoup解析對(duì)象
soup = BeautifulSoup(html_doc, "html.parser", from_encoding="utf-8")
# 獲取所有的鏈接
links = soup.find_all('a')
print("所有的鏈接")
for link in links:
print(link.name, link['href'], link.get_text())
print("獲取特定的URL地址")
link_node = soup.find('a', href="http://example.com/elsie")
print(link_node.name, link_node['href'], link_node['class'], link_node.get_text())
運(yùn)行結(jié)果如下:
所有的鏈接
a http://example.com/elsie Elsie
a http://example.com/lacie Lacie
a http://example.com/tillie Tillie
獲取特定的URL地址
a http://example.com/elsie ['sister'] Elsie
selenium實(shí)現(xiàn)
Selenium是廣泛使用的模擬瀏覽器運(yùn)行的庫,它是一個(gè)用于Web應(yīng)用程序測試的工具。 Selenium測試直接運(yùn)行在瀏覽器中,就像真正的用戶在操作一樣,并且支持大多數(shù)現(xiàn)代 Web 瀏覽器。因此可以利用Selenium進(jìn)行爬蟲操作。
selenium官網(wǎng):https://www.selenium.dev/zh-cn/
selenium庫安裝
pip install selenium
瀏覽器驅(qū)動(dòng)下載:
針對(duì)不同的瀏覽器,需要安裝不同的驅(qū)動(dòng)。下面以安裝 Chrome 驅(qū)動(dòng)作為演示。
查看瀏覽器版本
點(diǎn)擊設(shè)置,找到“關(guān)于Chrome”,即可查看瀏覽器的版本。
下載驅(qū)動(dòng)
下載地址:https://chromedriver.storage.googleapis.com/index.html
下載地址2:https://chromedriver.chromium.org/downloads
選擇chrome版本對(duì)應(yīng)的驅(qū)動(dòng)下載
環(huán)境變量配置
將下載的驅(qū)動(dòng)復(fù)制到chrome的安裝目錄下
配置環(huán)境變量
此電腦——屬性——高級(jí)系統(tǒng)設(shè)置——環(huán)境變量——用戶變量——Path——新建——復(fù)制粘貼chrome安裝路徑——確定
運(yùn)行測試
運(yùn)行如下代碼,如果能彈出Chrome瀏覽器,則說明安裝成功。文章來源:http://www.zghlxwxcb.cn/news/detail-729966.html
from selenium import webdriver
# Chrome瀏覽器
driver = webdriver.Chrome()
代碼實(shí)戰(zhàn)
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Chrome()
url = 'https://www.csdn.net/'
browser.get(url)
titles = browser.find_elements(By.CLASS_NAME, 'navigation-right')
for item in titles:
print(item.text)
運(yùn)行結(jié)果如下:文章來源地址http://www.zghlxwxcb.cn/news/detail-729966.html
后端
前端
移動(dòng)開發(fā)
編程語言
Java
Python
人工智能
AIGC
大數(shù)據(jù)
數(shù)據(jù)庫
數(shù)據(jù)結(jié)構(gòu)與算法
音視頻
云原生
云平臺(tái)
到了這里,關(guān)于Python爬蟲實(shí)現(xiàn)(requests、BeautifulSoup和selenium)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!