我问过一个类似的问题,关于从https://ethnicelebs.com/all-celeb用静态url导航多个页面,谢谢你的帮助!但是现在我想通过点击每个名字来获取每个角色的种族信息。我现在可以浏览所有页面,但我的代码一直从第一页抓取信息。你知道吗
我试过以下方法:
url = 'https://ethnicelebs.com/all-celeb'
driver = webdriver.Chrome()
driver.get(url)
while True:
page = requests.post('https://ethnicelebs.com/all-celebs')
soup = BeautifulSoup(page.text, 'html.parser')
for href in soup.find_all('a', href=True)[18:]:
print('Found the URL:{}'.format(href['href']))
request_href = requests.get(href['href'])
soup2 = BeautifulSoup(request_href.content)
for each in soup2.find_all('strong')[:-1]:
print(each.text)
Next_button = (By.XPATH, "//*[@title='Go to next page']")
WebDriverWait(driver, 50).until(EC.element_to_be_clickable(Next_button)).click()
url = driver.current_url
time.sleep(5)
(感谢@Sureshmani!)你知道吗
我希望代码在导航时能抓取每一页,而不仅仅是第一页。如何在当前页面继续导航时刮取它?谢谢!你知道吗
由于上一个答案中的嵌套循环,我误解了你的问题。以下代码将起作用:
在您的代码中,您只在开始时通过selenium发送一次请求,然后稍后使用
requests
。要同时导航和刮取一个页面,应该只使用上面示例中的selenium。你知道吗相关问题 更多 >
编程相关推荐