【发布时间】:2021-10-02 03:03:40
【问题描述】:
我正在尝试从网站 KKP 抓取数据。这些表存在一些页面,但所有页面都使用相同的 url。我对网络抓取数据非常陌生。我目前正在使用 selenium 来抓取表格,但它只包含一个第一页,但我想抓取所有页面。有些日子我总是尝试通过谷歌搜索并在 youtube 上查看教程,但我仍然卡住了。链接和我当前的代码如下。如果您使用 BeautifulSoup 来抓取数据,我没有问题。我希望有一个人可以帮助我。谢谢
网站:http://www.ppk-kp3k.kkp.go.id/direktori-pulau/index.php/public_c/propinsi/11
from selenium import webdriver
from kora.selenium import wd
wd.get("http://www.ppk-kp3k.kkp.go.id/direktori-pulau/index.php/public_c/propinsi/11")
wd.maximize_window()
with open('pulau_scrapping_aceh_lengkap4.csv','w') as file:
file.write('Pulau; Provinsi; Kabupaten \n')
pulau = wd.find_elements_by_xpath('/html/body/div[3]/div/div/table/tbody/tr/td[1]')
provinsi = wd.find_elements_by_xpath('//*[@id="DataTables_Table_0"]/tbody/tr/td[2]')
kabupaten = wd.find_elements_by_xpath('//*[@id="DataTables_Table_0"]/tbody/tr/td[3]')
row_data = []
for k in range(6):
with open('pulau_scrapping_aceh_lengkap4.csv','a') as file:
for i in range(len(pulau)):
file.write(pulau[i].text + ';' + provinsi[i].text + ';' + kabupaten[i].text + '\n')
next = wd.find_element_by_xpath('//*[@id="DataTables_Table_0_last"]')
next.click()
file.close()
wd.close()
【问题讨论】:
标签: python selenium url web-scraping beautifulsoup