我想通过selenium爬取一个问答的内容,但是它没有重定向到新的页面,所以我要如何去操作它呢,比如我爬取的第一个问诊的对话,如何返回上一页继续爬取下一个会话,折磨我一天了求帮忙
from selenium import webdriver
from selenium.webdriver.common.by import By
import time, csv
class CYYS:
def __init__(self, doc):
self.doc = doc
# 主函数
def run(self):
driver = webdriver.Chrome()
driver.implicitly_wait(10)
# 爬取网址
driver.get("https://m.chunyuyisheng.com/qalist/?page=1&high_quality=0")
driver.find_element(By.CSS_SELECTOR, ".hot-qa-list").click()
time.sleep(1)
self.handler(driver)
# 返回会话页面,继续处理
# driver.back()
# 对页面进行处理
def handler(self, driver):
driver.execute_script('window.scrollBy(0,10000)')
driver.find_element(By.CSS_SELECTOR, ".showmore-btn").click()
sessions = driver.find_elements(By.CSS_SELECTOR, ".block-right")
for session in sessions:
txt = session.find_elements(By.TAG_NAME, 'p')
stringFilelds = [field.text for field in txt]
print(stringFilelds)
# self.Save(doc=self.doc, txt=stringFilelds)
# def Save(self, doc, txt):
# with open(self.doc, 'w', encoding='utf-8') as f:
# f.write(str(txt) + '\n')
CYYS(doc='1.txt').run()