如何使用静态url刮取多个页面,请求方法get

2024-04-16 17:26:37 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,对不起我的英语,其次我只有两个星期大

现在我使用python、模块selenium和chromedriver,我想刮取的页面是“http://lpse.maroskab.go.id/eproc4/lelang“,我使用的代码是:

from time import sleep
from selenium import webdriver
from bs4 import BeautifulSoup as bs
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("disable-extensions")
chrome_options.add_argument("disable-gpu")
chrome_options.add_argument("headless")

path =r'F:\python latian\webdriver\chromedriver.exe'

driver = webdriver.Chrome(options=chrome_options, executable_path = path)
driver.get('http://lpse.maroskab.go.id/eproc4/lelang')
sleep(5)
page=bs(driver.page_source,"html.parser")
code=page.find_all(class_="sorting_1")
for xx in code:
   kode=xx.contents[0]
   print(code)

但是有了这段代码,我只从第一页获取数据,我想完成的是删除另一页, 然后我遇到了([此线程][1]),但在该线程中请求方法的答案是“post”,在我的线程中是“get”。我在那里读到一条使用“urllib.request”的建议,但据我所知,这种方法只有在我知道url的情况下才有效。 非常感谢。 [1]: https://stackoverflow.com/questions/48985758/how-to-scrape-multiple-pages-with-an-unchanging-url-python-3


Tags: pathfromimportaddhttpdriverseleniumpage
1条回答
网友
1楼 · 发布于 2024-04-16 17:26:37

有很多方法可以做到这一点,并且在多个页面上进行迭代并不简单,您的代码将需要相当大的改进。由于您是新手,我将介绍您需要包含的内容,并给出一个示例,您可以使用它将其合并到代码中

您肯定需要使用Explicit Waits来等待“加载”指示器的不可见性

您还需要一个无休止的循环,只有当“下一页”链接被禁用(没有更多可用页面)时,我们才会退出该循环

This是一个很好的例子,使用@alecxe的答案

相关问题 更多 >