【问题标题】:Extract WebTable using selenium使用 selenium 提取 WebTable
【发布时间】:2021-07-20 10:08:51
【问题描述】:

我想从https://nepsealpha.com/ 中提取一个包含许多表的特定表。显示所需表格的图片。使用以下代码更改表索引不显示所需的表。索引可以从 0 更改为 12,但未显示所需的表。帮我。 Required Table Image

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from time import sleep
from bs4 import BeautifulSoup
import pandas as pd

options = Options()
options.add_argument('--allow-running-insecure-content')
options.add_argument('--ignore-certificate-errors')
options.add_argument("--headless")
options.add_argument("--disable-extensions")
options.add_argument("--disable-gpu")
driver = webdriver.Chrome(executable_path=r"C:/Users/Personal/WebDriver/chromedriver.exe", options=options)
accept_untrusted_certs = True

driver.get('https://nepsealpha.com/')
sleep(5)
soup=BeautifulSoup(driver.page_source,'lxml')
table = soup.find_all('table')[0]
df = pd.read_html(str(table),header=0)
print(df)

【问题讨论】:

    标签: python-3.x selenium-webdriver web-scraping html-table


    【解决方案1】:

    通过唯一的 id 选择表

    table  = soup.find(id="articlebody")
    rows = table.tbody.findAll("tr")
    for row in rows:
        cells = row.findAll("td")
        print cells
    

    【讨论】:

    • table = soup.find(id="info-table0") print(table) #results 无。我没有得到 Nischitha!
    • 遍历单元格并按上述方式打印
    • 亲爱的 Nischitha,您能否为我测试并粘贴工作代码。有很多表,但我想提取的表没有像我想的那样工作。我的需求表如此链接i.stack.imgur.com/DDFuM.png 所示(也在上面)。
    【解决方案2】:

    你需要的表格是通过JS加载的。我们需要滚动页面以执行 js,所以这对我有用:-

    driver.get('https://nepsealpha.com/')
    driver.execute_script("window.scrollTo(0, 1000);")
    sleep(5)
    soup=BeautifulSoup(driver.page_source,'html5')
    
    df = pd.read_html(str(soup))
    df_required = df[1]
    

    【讨论】:

    • 亲爱的普拉哈尔,谢谢!正如我使用熊猫数据框所期望的那样。但是我想再问一件事“我可以像在其他情况下那样对不同的页面进行迭代,以防出现 ("window.scrollTo(0, 1000);"),我们可以看到多个页面 1,2,3 ,.. at bottom.while True: try:link=self.driver.find_element_by_link_text("Next") print('Moving Next Page') link.click().
    • 改变xpath //*[@id="scoreBoard"]/div/div/div[2]/div/a[3], //*[@id="scoreBoard"]/ div/div/div[2]/div/a[4], //*[@id="scoreBoard"]/div/div/div[2]/div/a[5] 在web中加载不同的表格值,但是我们上面的方法总是提取相同的值。我也想让表格迭代那些 xpath 更改并在这些页面中组合表格。
    • 这不是最初的问题。请针对您面临的问题以及您迄今为止尝试过的问题提出一个单独的问题。
    猜你喜欢
    • 1970-01-01
    • 2021-06-18
    • 2021-11-12
    • 1970-01-01
    • 2020-12-01
    • 2021-04-21
    • 1970-01-01
    • 2022-07-17
    • 2012-06-26
    相关资源
    最近更新 更多