ws从代码开始 2023-12-16 16:26 采纳率: 66.7%
浏览 12
已结题

请求网页的Request URL打印输出

selenium自动化打开豆瓣电影短评网页,怎么获取该短评页面的Request URL并返回打印


```python
import time
from selenium.webdriver.common.by import By
from selenium import webdriver

word = input('请你输入你需要找的电影:')

options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)

driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://movie.douban.com/')
driver.implicitly_wait(10)
time.sleep(3)

# 在搜索框中输入用户输入的电影名称
driver.find_element(By.XPATH,'//*[@id="inp-query"]').send_keys(word)
# 点击搜索按钮
driver.find_element(By.XPATH,'//*[@id="db-nav-movie"]/div[1]/div/div[2]/form/fieldset/div[2]/input').click()
time.sleep(1)
#点击该电影
driver.find_element(By.XPATH,'//*[@id="root"]/div/div[2]/div[1]/div[1]/div/div/div/div[1]/a').click()
time.sleep(1)
#点击短评
driver.find_element(By.XPATH,'//*[@id="comments-section"]/div[1]/h2/span/a').click()
time.sleep(3)


  • 写回答

1条回答 默认 最新

  • 渴口可乐 2023-12-16 16:43
    关注

    亲爱的,还是我,这次喂你嘴里了,可不许给我点踩。

    img

    
    
    import time
    from selenium.webdriver.common.by import By
    from selenium import webdriver
    
    word = input('请你输入你需要找的电影:')
    
    options = webdriver.ChromeOptions()
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    
    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.get('https://movie.douban.com/')
    driver.implicitly_wait(10)
    time.sleep(3)
    
    # 在搜索框中输入用户输入的电影名称
    driver.find_element(By.XPATH ,'//*[@id="inp-query"]').send_keys(word)
    
    # 点击搜索按钮
    driver.find_element(By.XPATH, '//*[@id="db-nav-movie"]/div[1]/div/div[2]/form/fieldset/div[2]/input').click()
    time.sleep(0.3)
    driver.find_element(By.XPATH, '//*[@id="root"]/div/div[2]/div[1]/div[1]/div/div/div/div[1]/a').click()
    time.sleep(0.3)
    driver.find_element(By.XPATH, '//*[@id="comments-section"]/div[1]/h2/span/a').click()
    
    for i in range(1,21):
        pl = driver.find_element(By.XPATH, f'//*[@id="comments"]/div[{i}]/div[2]/p/span').text
        print(f"{i}、{pl}")
    
    
    ##//*[@id="comments"]/div[1]/div[2]/p/span    第一条评论
    ##//*[@id="comments"]/div[20]/div[2]/p/span    最后一条评论
    
    
    
    
    
    
    
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月16日
  • 修改了问题 12月16日
  • 创建了问题 12月16日