获取 LinkedIn 帖子中总页数的方法

huangapple go评论62阅读模式
英文:

How to get the total number of pages present in a linkedin post

问题

我想要获取这篇LinkedIn帖子的总页数。

链接:
https://www.linkedin.com/feed/update/urn:li:activity:7050104978106974208/

这是我尝试悬停在帖子上查找总页数的代码:

post_url = "https://www.linkedin.com/feed/update/urn:li:activity:7050104978106974208"
browser.get(post_url)

browser.execute_script("window.scrollBy(0,900)")

achains = ActionChains(browser)
browser.find_element(By.XPATH, "//div[@class='ssplayer-actions center-actions']")
slider_dot = browser.find_element(By.XPATH, "//span[@aria-label='Total pages']")
achains.move_to_element(slider_dot).perform()

但是这段代码报错:
无法定位元素: {"method": "xpath", "selector": "//div[@class='ssplayer-actions center-actions']"}

请问有人能帮助我解决这个问题吗?

英文:

I want to get the total number of pages from this linkedin post

url:
https://www.linkedin.com/feed/update/urn:li:activity:7050104978106974208/

Here's the code that I tried to hover over the post and find the total pages

post_url = "https://www.linkedin.com/feed/update/urn:li:activity:7050104978106974208"
browser.get(post_url)

browser.execute_script("window.scrollBy(0,900)","") 

achains = ActionChains(browser)
browser.find_element(By.XPATH, "//div[@class='ssplayer-actions center-actions']")
slider_dot = browser.find_element(By.XPATH, "//span[@aria-label='Total pages']")
achains.move_to_element(slider_dot).perform()

But this code is throwing and error
Unable to locate element: {"method":"xpath","selector":"//div[@class='ssplayer-actions center-actions']"}

Can anyone please help me in solving this?

答案1

得分: 1

这是您可以尝试的方法,

在这里,我假设您已经登录。)

思路是我们需要切换到包含帖子附加文档详细信息的 iframe

获取 LinkedIn 帖子中总页数的方法

正如您在上面的HTML片段中所看到的,属性"aria-valuemax" 包含页面的最大值信息,即文档中的页数。

以下是解决方案:

post_url = "https://www.linkedin.com/feed/update/urn:li:activity:7050104978106974208"
browser.get(post_url)

browser.execute_script("window.scrollBy(0,900);")
WebDriverWait(browser, 10).until(
    EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[class='document-s-container__document-element document-s-container__document-element--loaded']")))

element = browser.find_element(By.CSS_SELECTOR, 'div.ssplayer-actions.center-actions')
pages = element.find_element(By.CSS_SELECTOR, 'div.ssplayer-progress-bar.meter-animated').get_attribute('aria-valuemax')
print(pages)

页数:

7
英文:

This is how you can try it,

(Here, I assume you're already logged in.)

The idea is that we need to switch to the iframe where the details of the attached document of the post lie.

获取 LinkedIn 帖子中总页数的方法

And as you can see in the above HTML snippet, the attribute "aria-valuemax" holds the information about the maximum value of the page i.e. the number of pages in the document.

Here is the solution:

post_url = "https://www.linkedin.com/feed/update/urn:li:activity:7050104978106974208"
browser.get(post_url)

browser.execute_script("window.scrollBy(0,900);")
WebDriverWait(browser, 10).until(
    EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[class='document-s-container__document-element document-s-container__document-element--loaded']")))

element = browser.find_element(By.CSS_SELECTOR, 'div.ssplayer-actions.center-actions')
pages = element.find_element(By.CSS_SELECTOR, 'div.ssplayer-progress-bar.meter-animated').get_attribute('aria-valuemax')
print(pages)

pages:

7

huangapple
  • 本文由 发表于 2023年4月11日 14:53:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983137.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定