3

このウェブサイトをスクレイピングしようとしています:

http://stats.uis.unesco.org/unesco/TableViewer/tableView.aspx?ReportId=210

Python と Selenium を使用します (以下のコードを参照)。コンテンツは動的に生成され、ブラウザに表示されないデータは読み込まれないようです。ブラウザ ウィンドウを大きくして、ページの一番下までスクロールしてみました。ウィンドウを拡大すると、必要なすべてのデータが水平方向に取得されますが、垂直方向にはスクレイピングするデータがまだたくさんあります。スクロールはまったく機能していないようです。

これを行う方法について明るいアイデアを持っている人はいますか?

ありがとう!

from selenium import webdriver
import time

url = "http://stats.uis.unesco.org/unesco/TableViewer/tableView.aspx?ReportId=210"
driver = webdriver.Firefox()
driver.get(url)
driver.set_window_position(0, 0)
driver.set_window_size(100000, 200000)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

time.sleep(5) # wait to load

soup = BeautifulSoup(driver.page_source)

table = soup.find("table", {"id":"DataTable"})

### get data
thead = table.find('tbody')
loopRows = thead.findAll('tr')
rows = []
for row in loopRows:
rows.append([val.text.encode('ascii', 'ignore') for val in  row.findAll(re.compile('td|th'))])
with open("body.csv", 'wb') as test_file:
  file_writer = csv.writer(test_file)
  for row in rows:
      file_writer.writerow(row)
4

2 に答える 2

0

でスクロールできます

self.driver.find_element_by_css_selector("html body.TVTableBody table#pageTable tbody tr td#cell4 table#MainTable tbody tr td#vScrollTD img[onmousedown='imgClick(this.sbar.visible,this,event);']").click()

スクロールできるようになったら、何かが欠けていない限り、スクレイピングはかなり標準的なはずです

于 2013-05-28T02:33:00.853 に答える