首页 > 解决方案 > 用 selenium 和 python 抓取

问题描述

我试图将我的脚趾浸入硒的世界,但我在理解事物的工作原理时遇到了问题。

首先我只是想学习抓取网站。

以这个网站为例

https://udemycoupons.me/

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

现在我可以在 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)

我如何用硒做到这一点?

我似乎无法以相同的方式检索对象

帮助 !!:)

标签: pythonseleniumselenium-webdriverweb-scraping

解决方案


你可以从:

# 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。


推荐阅读