对于该网站,需要使用Selenium和Chrome浏览器来模拟用户行为,才能获取到完整的页面内容。以下是使用Python和Selenium模拟浏览器并使用XPath提取表格数据的示例代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from lxml import etree

# 设置Chrome浏览器的可选项
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--disable-gpu')

# 创建Chrome浏览器对象
browser = webdriver.Chrome(options=options)

# 访问目标网页
url = 'https://xl.16888.com/style.html'
browser.get(url)

# 等待表格元素加载完成
table = WebDriverWait(browser, 10).until(
    EC.presence_of_element_located((By.XPATH, '//table[@class="table"]'))
)

# 使用XPath提取表格数据
html = browser.page_source
tree = etree.HTML(html)
rows = tree.xpath('//table[@class="table"]//tr')
for row in rows:
    cols = row.xpath('td//text()')
    print(cols)

# 关闭浏览器
browser.quit()

运行上述代码,即可获取到该网站上的表格数据。请注意,由于该网站的页面结构可能会发生变化,因此代码可能需要进行适当的修改才能正确运行。

用xpath爬取httpsxl16888comstylehtml网站的表格数据

原文地址: https://www.cveoy.top/t/topic/bFkk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录