亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

用 selenium 和 python 抓取

用 selenium 和 python 抓取

SMILET 2022-12-06 17:26:49
我試圖將我的腳趾浸入硒的世界,但我在理解事物的工作原理時遇到了問題。首先,我只是想學習抓取網站。以這個網站為例https://udemycoupons.me/我希望能夠抓取所有可用的優惠券并返回:標題、日期、url 鏈接?,F在我可以在 BeautifulSoup 中使用search_coupon = soup.find_all('div',{'class':'td_module_1 td_module_wrap td-animation-stack'})for coupon in search_coupon:    coupon_title = coupon.find('h3',{'class':'entry-title td-module-title'}).text    coupon_date = coupon.find('span',{'class':'td-post-date'}).text    coupon_url = coupon.find('a').get('href')    print(coupon_title, coupon_date, coupon_url)我如何使用硒來做到這一點?我似乎無法以同樣的方式檢索對象
查看完整描述

2 回答

?
智慧大石

TA貢獻1946條經驗 獲得超3個贊

你可以從:


# Definning some basic functions for later usage

def clickOnId(id):

    browser.find_element_by_id(id).click()


def clickOnXpath(xpath):

    browser.find_element_by_xpath(xpath).click()


def clickOnClass(class_name):

    browser.find_element_by_class_name(class_name).click()


def TypeInId(id,toBeTyped):

    elems = browser.find_elements_by_id(id)

    elems[0].send_keys(toBeTyped)


def TypeInXpath(xpath,toBeTyped):

    elems = browser.find_elements_by_xpath(xpath)

    elems[0].send_keys(toBeTyped)

并查看此代碼以進入 selenium。


查看完整回答
反對 回復 2022-12-06
?
蝴蝶不菲

TA貢獻1810條經驗 獲得超4個贊

要將 selenium 用于瀏覽器自動化,


首先,您需要下載適用于 Firefox 的chromedriver.exe或geckodriver.exe,并將其保存到某個位置。


其次,創建一個變量來保存瀏覽器 webdriver 路徑,例如,driver = webdriver.Chrome(executable_path=r'C:/path/to/chromedriver.exe')


第三,定義空列表來保存您從網絡上抓取的每個屬性的數據。像這樣的東西,coupon_title=[] #List to store coupon title。原因是,如果您有興趣稍后將此數據保存到數據框中以供進一步分析。


下面給出了一個最小的可重現示例:


from bs4 import BeautifulSoup

from selenium import webdriver


coupon_title=[] #List to store coupon title

coupon_date=[] #List to store coupon date

coupon_url=[] #List to store coupon url


driver = webdriver.Chrome(executable_path = r'C:/temp/chromedriver.exe')

driver.get("https://udemycoupons.me/")

content = driver.page_source

soup = BeautifulSoup(content, 'html.parser')

soup.prettify()

search_coupon = soup.find_all('div',{'class':'td_module_1 td_module_wrap td-animation-stack'})


for coupon in search_coupon:

    coupon_title = coupon.find('h3',{'class':'entry-title td-module-title'}).text

    coupon_date = coupon.find('span',{'class':'td-post-date'}).text

    coupon_url = coupon.find('a').get('href')

    print(coupon_title, coupon_date, coupon_url)

結果是:

哈佛在線免費課程 | 65 門免費課程 2020 年 4 月 26 日 https://udemycoupons.me/harvard-free-course-online/ 750 門免費 Udemy 課程 | 2020 年 4 月更新 2020 年 4 月 13 日 https://udemycoupons.me/750-free-udemy-course-apr-2020-updated/ 100% 免費 Udemy 優惠券 | 如何在 TikTok 上賺錢 2020 年 5 月 24 日 https://udemycoupons.me/100-free-udemy-coupon-how-to-make-money-on-tiktok/ 100% 免費 | JSON – JSON 數據 JavaScript 快速介紹... 2020 年 5 月 24 日 https://udemycoupons.me/100-free-json-quick-introduction-to-json-data-javascript-using-json/ 100% 免費 Udemy 優惠券| 完成社交媒體營銷 2020 年 5 月 24 日 https://udemycoupons.me/100-free-udemy-coupon-complete-social-media-marketing/ 100% OFF Udemy 優惠券 | 2020 年數字戰略成功 2020 年 5 月 24 日 https://udemycoupons.me/100-off-digital-strategy-success-2020/ 100% OFF | 使用 Python 進行時間序列分析和預測 2020 年 5 月 24 日 https://udemycoupons.me/time-series-analysis-and-forecasting-using-python-100-off/ 100% OFF | 在家工作的工具 – Google Apps,... 2020 年 5 月 24 日 https://udemycoupons.me/100-off-tools-for-working-from-home-google-apps-trello-zoom/ 100% 折扣 | 機器學習基礎知識:邏輯回歸、LDA 和 KNN ... 2020 年 5 月 24 日 https://udemycoupons.me/machine-learning-basics-logistic-regression-lda-knn-in-r/ 100% 折扣 | CNN 使用 Keras 和 TensorFlow 進行計算機視覺... 2020 年 5 月 24 日 https://udemycoupons.me/100-off-cnn-for-computer-vision-with-keras-and-tensorflow-in-python/ 100% 折扣 | 機器學習:邏輯回歸、LDA 和 K-NN 在... 2020 年 5 月 24 日 https://udemycoupons.me/logistic-regression-lda-k-nn-in-python-machine-learning-100-off/ 100 % 折扣 | 使用 R Studio 完成機器學習 – ML... 2020 年 5 月 24 日 https://udemycoupons.me/100-off-complete-machine-learning-with-r-studio-ml-for-2020/ 100% OFF | Adobe Premiere Pro:初學者視頻編輯 2020 年 5 月 24 日 https://udemycoupons.me/100-off-adobe-premiere-pro-ultimate-beginner-course/ 100% 折扣 | Python 初學者 – 學習所有基礎知識... 2020 年 5 月 24 日 https://udemycoupons.me/100-off-python-for-beginners-learn-all-the-basics-of-python/ 100% OFF Udemy 優惠券 | 學習 Angular 的指南... 2020 年 5 月 24 日 https://udemycoupons.me/100-off-udemy-coupon-learn-angular/ Udemy 優惠券 100% OFF | 2020 年 Python 課程!2020 年 5 月 24 日 https://udemycoupons.me/100-off-udemy-coupon-the-python-course-2020/ Udemy 優惠券 100% OFF | 2020 年數據科學課程 2020 年 5 月 24 日 https://udemycoupons.me/100-off-udemy-coupon-the-data-science-course-2020/ 100% OFF | 如何使用 Gamemaker Studio 2 制作游戲... 2020 年 5 月 24 日 https://udemycoupons.me/100-off-how-to-make-games-with-gamemaker-studio-2-using-gml/ 100% 折扣 | 數據科學和機器學習所需的 Python ... 2020 年 5 月 24 日 https://udemycoupons.me/100-off-python-required-for-data-science-and-machine-learning-2020/ 100% OFF Udemy 優惠券| 學習 Microsoft OneDrive 2020 年 5 月 24 日 https://udemycoupons.me/100-off-udemy-coupon-learn-microsoft-onedrive/

希望這可以幫助。



查看完整回答
反對 回復 2022-12-06
  • 2 回答
  • 0 關注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號