【发布时间】: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