用硒和蟒蛇刮

时间:2020-05-24 20:58:53

标签: python selenium selenium-webdriver web-scraping

我试图将自己的脚趾伸入硒的世界,并在理解事物的工作方式方面遇到问题。

首先,我只是尝试学习抓取网站。

以该网站为例

https://udemycoupons.me/

我希望能够抓取所有可用的优惠券并返回:标题,日期,URL链接。

现在我可以在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 个答案:

答案 0 :(得分:1)

您可以开始:

input

并查看this进入硒的代码。

答案 1 :(得分:0)

要使用硒来实现浏览器自动化,

首先,您需要下载chromedriver.exegeckodriver.exe for firefox并将其保存到某个位置。

第二,创建一个变量来保存浏览器的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快速入门... 5月 2020年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/ Udemy优惠券100%OFF | Digital Strategy Success 2020 2020年5月24日 https://udemycoupons.me/100-off-digital-strategy-success-2020/ 100% 关闭|使用Python进行时间序列分析和预测2020年5月24日 https://udemycoupons.me/time-series-analysis-and-forecasting-using-python-100-off/ 100%折扣|在家工作的工具– 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%折扣|使用Keras和TensorFlow的CNN用于计算机视觉...五月 2020年24月 https://udemycoupons.me/100-off-cnn-for-computer-vision-with-keras-and-tensorflow-in-python/ 100%折扣|机器学习:Logistic回归,LDA和K-NN在...中 2020年24月 https://udemycoupons.me/logistic-regression-lda-k-nn-in-python-machine-learning-100-off/ 100%折扣|使用R Studio – ML进行完整的机器学习5月24日, 2020年 https://udemycoupons.me/100-off-complete-machine-learning-with-r-studio-ml-for-2020/ 100%折扣| Adobe Premiere Pro:面向初学者的视频编辑5月24日, 2020年 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/ Udemy优惠券100%OFF |从...学习角度的指南... 2020年5月24日 https://udemycoupons.me/100-off-udemy-coupon-learn-angular/ 100%折扣 Udemy优惠券| Python课程2020! 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%折扣|如何使用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/ Udemy优惠券100%OFF | Learn Microsoft OneDrive 2020年5月24日 https://udemycoupons.me/100-off-udemy-coupon-learn-microsoft-onedrive/

希望这会有所帮助。