VICTORY^ 2023-04-04 16:31 采纳率: 100%
浏览 30
已结题

用xpath解析返回为空列表

想要通过selenium和xpath获取“中招体育考试”百度资讯每条新闻的内容,但一直返回列表不知道哪里出现了问题。
初学者,求各位指点

from lxml import etree
from time import sleep
from selenium.webdriver import ChromeOptions
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
option = ChromeOptions()
option.add_experimental_option('excludeSwitches',['enable-automation'])
s = Service(executable_path=r'C:\Users\86198\Desktop\python基础\chromedriver.exe')
brs = webdriver.Chrome(service=s)
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'}
brs.get('https://www.baidu.com/')
sleep(1)
brs.find_element(By.XPATH,r'//*[@id="kw"]').send_keys('中招体育考试')
brs.find_element(By.XPATH,r'//*[@id="su"]').click()
sleep(2)
brs.find_element(By.XPATH,r'//*[@id="s_tab"]/div/a[1]').click()        # 点击资讯
brs.find_element(By.XPATH,r'//*[@id="header_top_bar"]/div[2]/span').click()
brs.find_element(By.XPATH,r'//*[@id="c-tips-container"]/div[1]/div/div/ul[2]/li/a').click()      # 点击按时间排序
brs.find_element(By.XPATH,r'//*[@id="header_top_bar"]/div[1]/div/span').click()
brs.find_element(By.XPATH,r'//*[@id="c-tips-container"]/div[2]/div/div/ul/li[3]/a').click()   # 点百家号
sleep(2)

labels = brs.find_elements(By.XPATH,r'//div[@id="content_left"]/div[@class="result-op c-container xpath-log new-pmd"]')
length = len(labels)
for i in range(0,length):
    labels[i].find_element(By.XPATH,r'.//h3/a').click()
    sleep(5)

    text = brs.page_source
    html = etree.HTML(text)
    title = html.xpath('//*[@id="ssr-content"]/div[2]/div/div[1]/div[1]/div/div[1]/text()')
    source = html.xpath('//*[@id="ssr-content"]/div[2]/div/div[1]/div[1]/div/div[2]/div[2]/a/p/text()')
    print(title,source)
    sleep(5)

  • 写回答

1条回答 默认 最新

  • Dick_不周 2023-04-04 17:54
    关注

    效果如下

    img

    问题是点击链接之后没有跳转到对应窗口
    所以找不到对应的XPATH
    这边也是写了跳转和返回的逻辑
    构建对象的路径还需要你这边改回来
    ,应该对你有所帮助
    有用请采纳

    from lxml import etree
    from time import sleep
    from selenium.webdriver import ChromeOptions
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.chrome.service import Service
    
    option = ChromeOptions()
    option.add_experimental_option('excludeSwitches', ['enable-automation'])
    brs =webdriver.Chrome('D:\\20220526\\chromedriver.exe')
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'}
    brs.get('https://www.baidu.com/')
    sleep(1)
    brs.find_element(By.XPATH, r'//*[@id="kw"]').send_keys('中招体育考试')
    brs.find_element(By.XPATH, r'//*[@id="su"]').click()
    sleep(2)
    brs.find_element(By.XPATH, r'//*[@id="s_tab"]/div/a[1]').click()  # 点击资讯
    brs.find_element(By.XPATH, r'//*[@id="header_top_bar"]/div[2]/span').click()
    brs.find_element(By.XPATH, r'//*[@id="c-tips-container"]/div[1]/div/div/ul[2]/li/a').click()  # 点击按时间排序
    brs.find_element(By.XPATH, r'//*[@id="header_top_bar"]/div[1]/div/span').click()
    brs.find_element(By.XPATH, r'//*[@id="c-tips-container"]/div[2]/div/div/ul/li[3]/a').click()  # 点百家号
    sleep(2)
    
    labels = brs.find_elements(By.XPATH, r'//div[@id="content_left"]/div[@class="result-op c-container xpath-log new-pmd"]')
    length = len(labels)
    sousuo_window = brs.current_window_handle
    for i in range(0, length):
        if brs.current_window_handle != sousuo_window :
            brs.close()
            brs.switch_to.window(sousuo_window)
    
        labels[i].find_element(By.XPATH, r'.//h3/a').click()
        sleep(5)
        for window_handles in brs.window_handles :
            if brs.current_url != window_handles :
                brs.switch_to.window(window_handles)
        text = brs.page_source
        html = etree.HTML(text)
        title = html.xpath('//*[@id="ssr-content"]/div[2]/div/div[1]/div[1]/div/div[1]/text()')
        source = html.xpath('//*[@id="ssr-content"]/div[2]/div/div[1]/div[1]/div/div[2]/div[2]/a/p/text()')
        print(title, source)
        sleep(5)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月15日
  • 已采纳回答 4月7日
  • 创建了问题 4月4日