Python 3 & Selenium - 如何组合多个 For 循环 - 抓取 Web Table



我是Python(和Stack Overflow(的新手,我正在尝试找到一种有效的方法,从具有多个页面的内部网站上的表中提取数据。

以下代码工作正常,但由于该网站有 16 个页面,我需要找到一种方法,简单地说这个 For Loop。

任何帮助将不胜感激。谢谢

\
first = '//*[@id="container"]/table/tbody/tr['
second ="]/td["
last = "]"
for n in range(1,rows+1):
for m in range(1,cols+1):
final_path = (first+str(n)+second+str(m) + last)
table = driver.find_element_by_xpath(final_path).text
print(table, end= " ")
print()
#  - Click on Page 2 and get all the table data from there.
driver.find_element_by_link_text("2").click()
for n in range(1,rows+1):
for m in range(1,cols+1):
final_path = (first+str(n)+second+str(m) + last)
table = driver.find_element_by_xpath(final_path).text
print(table, end= " ")
print()
#  - Click on Page 3 and get all the table data from there.
driver.find_element_by_link_text("3").click()
for n in range(1,rows+1):
for m in range(1,cols+1):
final_path = (first+str(n)+second+str(m) + last)
table = driver.find_element_by_xpath(final_path).text
print(table, end= " ")
print()
\

谢谢。

您可以添加另一个封闭的 for 循环。

for page_no in range(2, 17): # 2...16
driver.find_element_by_link_text(str(page_no)).click()
for n in range(1,rows+1):
for m in range(1,cols+1):
final_path = (first+str(n)+second+str(m) + last)
table = driver.find_element_by_xpath(final_path).text
print(table, end= " ")
print()

相关内容

  • 没有找到相关文章

最新更新