【问题标题】:I like to get table text from <tr> <td> using selenium我喜欢使用 selenium 从 <tr> <td> 获取表格文本
【发布时间】:2018-04-05 13:52:01
【问题描述】:

这是我目前尝试过的试验。

from urllib.request import urlopen
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException

url ='http://marketdata.krx.co.kr/mdi#document=080120&547c5e15ef32e37dc099b89d69ac8970-[object%20HTMLDivElement]=1&547c5e15ef32e37dc099b89d69ac8970-[object%20HTMLDivElement]=2&547c5e15ef32e37dc099b89d69ac8970-[object%20HTMLDivElement]=1&547c5e15ef32e37dc099b89d69ac8970-object%20HTMLDivElement]=1'

driver = webdriver.Chrome()
driver.get(url)
element = driver.find_element_by_xpath('//select[@name="upclss"]')
all_options = element.find_elements_by_tag_name("option")
for option in all_options :
if option.text == "원자재":
    option.click()
    driver.implicitly_wait(5)
    another = driver.find_element_by_xpath('//li[@class="active"]')
    another.click()
    driver.implicitly_wait(5)
    html = driver.page_source
    soup = BeautifulSoup(html, "html.parser")
    table = soup.findChildren('table')[0]
    rows = table.findChildren('tr')
    for row in rows:
        cells = row.findChildren('td')
        for cell in cells:
            cell_content = cell.getText()
            print(cell_content)

我应该怎么做才能从上面的 url 获取下面的表格内容并打印出来? 非常感谢!!

【问题讨论】:

  • 您能否包含您尝试获取的 HTML 的 sn-p?
  • 这是关于 ETF 的,有点像基金。下表显示代码、公司、ETF 名称等。我正在尝试自动化查询,所以我选择“원자재”,这是“原材料”的韩语单词。我成功地自动点击它......但无法获取实际数据。
  • 作为第一个猜测,我建议您查看html 的结果并确保您的HTML 正确通过。你为什么使用selenium?你不能用urlopen提取HTML吗?
  • 我尝试选择查询,即 ETF 的类型,然后刷新网络浏览器。想想看,我好像一开始就用了同一个html源

标签: python selenium web-scraping beautifulsoup


【解决方案1】:

为什么不从页面源获取它?我知道您使用的是 python,但在 Java 中我会以这种方式解决这个问题:

我会将页面源作为字符串处理并获得一个以&lt;table&gt; 开头并以 whith 结尾的子字符串 &lt;/table&gt; 或任何你想要的......

从此我会以同样的方式提取我想要的值 - 构建一个以 &lt;td&gt;-tag 开头并以 &lt;/td&gt;-tag 结尾的子字符串。

剩下的文字就是你在网页上看到的表格数据文字。

【讨论】:

  • 谢谢,但我对 Java 还很陌生..你能详细说明一下吗??
  • 很难说...我从没用过python。让我这样说:您需要找到一种方法来使用python获取字符串的字符串。 selenium的webdriver API可以为你打印页面源:Driver.getPageSource();
  • 将其输出保存在字符串变量中并从中提取值。
【解决方案2】:

html = driver.page_source 的输出(值)会有所帮助,但我认为这也可以:

from urllib.request import urlopen
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException

url ='http://marketdata.krx.co.kr/mdi#document=080120&547c5e15ef32e37dc099b89d69ac8970-[object%20HTMLDivElement]=1&547c5e15ef32e37dc099b89d69ac8970-[object%20HTMLDivElement]=2&547c5e15ef32e37dc099b89d69ac8970-[object%20HTMLDivElement]=1&547c5e15ef32e37dc099b89d69ac8970-object%20HTMLDivElement]=1'

driver = webdriver.Chrome()
driver.get(url)
element = driver.find_element_by_xpath('//select[@name="upclss"]')
all_options = element.find_elements_by_tag_name("option")
for option in all_options :
    if option.text == "원자재":
        option.click()
        driver.implicitly_wait(5)
        another = driver.find_element_by_xpath('//li[@class="active"]')
        another.click()
        driver.implicitly_wait(5)                       
        tds = driver.find_element_by_xpath("//table/tr/td")
        for td in tds :
            print(td.text)

【讨论】:

  • 不幸的是,它似乎无法在我的计算机上运行。 T-T
【解决方案3】:

最后,它在硒中解决了,而不是通过汤...

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
import time

url = '...'
element = driver.find_element_by_xpath('//select[@name="upclss"]')
all_options = element.find_elements_by_tag_name("option")
for option in all_options :
    print(option.text)
    option.click()
    driver.implicitly_wait(5)
    another = driver.find_element_by_xpath('//li[@class="active"]')
    another.click()
    time.sleep(5)
    header = driver.find_element_by_xpath('//table[@class="CI-GRID-HEADER-TABLE"]').text
    other = driver.find_element_by_xpath('//table[@class="CI-GRID-BODY-TABLE"]').text
    print(header)
    print(other)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 2018-12-14
    • 1970-01-01
    相关资源
    最近更新 更多