【问题标题】:scraping page links using selenium always return limited number of links使用 selenium 抓取页面链接总是返回有限数量的链接
【发布时间】:2021-08-28 19:47:53
【问题描述】:

我想从此页面“https://m.aiscore.com/basketball/20210610”中抓取所有匹配项链接,但只能获得有限数量的匹配项: 我试过这段代码:

import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("--headless") 
driver = webdriver.Chrome(executable_path=r"C:/chromedriver.exe", options=options)

url = 'https://m.aiscore.com/basketball/20210610'
driver.get(url)

driver.maximize_window()
driver.implicitly_wait(60) 

driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")    

soup = BeautifulSoup(driver.page_source, 'html.parser')

links = [i['href'] for i in soup.select('.w100.flex a')]
links_length = len(links) #always return 16
driver.quit()

当我运行代码时,我总是只得到 16 个匹配的链接,但页面有 35 个匹配。 我需要获取页面中的所有匹配链接。

【问题讨论】:

  • 您在我的 ubuntu 机器中的代码运行良好(我只更改了 chrome 可执行路径)。你用的是什么版本的chromedriver?
  • 我使用的是 91 版
  • 您要我给您获取该页面上所有链接的代码吗?

标签: python selenium-webdriver web-scraping beautifulsoup


【解决方案1】:

由于网站在滚动时正在加载,我尝试一次滚动一个屏幕,直到我们需要滚动到的高度大于窗口的总滚动高度。

我使用set 来存储匹配链接以避免添加已经存在的匹配链接。

在运行此程序时,我能够找到所有链接。希望这对您也有用。

import requests
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("--headless") 
driver = webdriver.Chrome(executable_path=r"C:\Users\User\Downloads\chromedriver.exe", options=options)

url = 'https://m.aiscore.com/basketball/20210610'
driver.get(url)
# Wait till the webpage is loaded
time.sleep(2)

# wait for 1sec after scrolling
scroll_wait = 1

# Gets the screen height
screen_height = driver.execute_script("return window.screen.height;")
driver.implicitly_wait(60) 

# Number of scrolls. Initially 1
ScrollNumber = 1

# Set to store all the match links
ans = set()

while True:
    # Scrolling one screen at a time until
    driver.execute_script(f"window.scrollTo(0, {screen_height * ScrollNumber})")
    ScrollNumber += 1
    
    # Wait for some time after scroll
    time.sleep(scroll_wait)
    
    # Updating the scroll_height after each scroll
    scroll_height = driver.execute_script("return document.body.scrollHeight;")
    
    # Fetching the data that we need - Links to Matches
    soup = BeautifulSoup(driver.page_source, 'html.parser')
    for j in soup.select('.w100 .flex a'):
        if j['href'] not in ans:
            ans.add(j['href'])
    # Break when the height we need to scroll to is larger than the scroll height
    if (screen_height) * ScrollNumber > scroll_height:
        break
    
    
print(f'Links found: {len(ans)}')
Output:

Links found: 61

【讨论】:

  • return Links found: 0, the driver didn't open the website (Server error An error occurred in the application and your page could not be serving. 如果您是应用程序所有者,请检查您的日志详细信息。)
  • @khaled koubaa 出现服务器错误。即使我在使用浏览器时遇到服务器错误。可能是 URL (m.aiscore.com/basketball/20210610) 不再有效。您可以使用其他 URL 进行检查。
  • @khaledkoubaa 我已经用这个 URL (m.aiscore.com/basketball) 检查了我的代码,我正在从中获取所有链接。
  • @khaledkoubaa 你看我之前的评论了吗?
  • 我需要抓取特定日期,而不仅仅是m.aiscore.com/basketball
【解决方案2】:

您没有在代码中添加任何隐含的等待。你可能想从那里开始。但是尝试使用driver.find_elements_by_link_text() 除了添加一些睡眠时间,这应该会为您创建一个列表。

【讨论】:

    猜你喜欢
    • 2020-09-21
    • 1970-01-01
    • 2011-06-25
    • 2014-01-10
    • 1970-01-01
    • 2020-11-17
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多