在窗体中选择项并提取出现的表

2024-04-25 05:15:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从一个网页中提取信息,它要求我从下拉列表中进行选择,并根据选择一个表格出现各种信息。我在页面上有一个表单/列表的选择值列表,我想遍历并提取表信息。你知道吗

网页:https://www.mcafee.com/enterprise/en-us/support/product-eol.html

import scrapy
from scrapy.spiders import Spider

product_names = ['Host Intrusion Prevention','McAfee Agent','Active Response','Database Security']

class McAfee_Spider(scrapy.Spider):
    name = 'McAfee'
    allowed_domains = 'mcafee.com'
    start_urls = 'https://www.mcafee.com/enterprise/en-us/support/product-eol.html'

    for product in product_names:
        def parse(self, response):
            scrapy.FormRequest.from_response(
            response,
            formxpath="//form[@id='selectProductArea']",
            formdata={
                "SelectProductArea" : product },
            clickdata = { "type": "select" },
            )

        def parse_table(self, response):
            product = response.xpath("//table[@class="general eoldynamicContent"]//tbody//tr//td[1]").extract()
            version = response.xpath("//table[@class="general eoldynamicContent"]//tbody//tr//td[2]").extract()
            eos_notif = response.xpath("//table[@class="general eoldynamicContent"]//tbody//tr//td[3]").extract()
            eol_date = response.xpath("//table[@class="general eoldynamicContent"]//tbody//tr//td[4]").extract()

我被困在如何形成提取XPath。我研究过的例子都有我可以访问的类,但是这个没有。此外,此网站要求我在表格出现之前从窗体/列表中单击,我使用的是“FORMCREQUEST.from\响应“但是我不确定我是否设置了正确的方法。你知道吗

我要提取的信息是产品名称、版本型号、支持结束通知和生命周期结束/支持结束信息。我想先将结果存储在数据框中,因为我需要连接其他来源的信息,然后导出到excel/csv。你知道吗

来自https://www.mcafee.com/enterprise/en-us/support/product-eol.html的“主机入侵防护”列表中第一个产品的预期结果

import pandas as pd
results = {'product':['McAfee Host Intrusion Prevention', 'McAfee Host Prevention for Linux'],
          'version':['8.0','8.0 Patch 6'],
          'eos_notif':['',''],
          'eol_date':['','']}
pd.DataFrame(results)

Tags: com信息列表responsetableproductxpathclass
1条回答
网友
1楼 · 发布于 2024-04-25 05:15:54

你找错地方了。在您选择列表中的任何内容后,上述网站不会发送任何FormRequest。相反,它从https://www.mcafee.com/enterprise/admin/support/eol.xml加载所有内容,只显示一段数据:

import scrapy


class McAfee_Spider(scrapy.Spider):
    name = 'McAfee'
    allowed_domains = 'mcafee.com'
    start_urls = ['https://www.mcafee.com/enterprise/admin/support/eol.xml']

    def parse(self, response):
        for product in response.xpath('//product'):
            product_title = product.xpath('./@title').get()
            for element in product.xpath('./element'):
                element_title = element.xpath('./@title').get()
                element_version = element.xpath('./@version').get()
                element_eos = element.xpath('./@eos').get()
                element_eos_notification = element.xpath('./@eos_notification').get()
                element_comment = element.xpath('./comment/text()').get()


                yield {
                    'product_title': product_title,
                    'element_title': element_title,
                    'element_version': element_version,
                    'element_eos': element_eos,
                    'element_eos_notification': element_eos_notification,
                    'element_commment': element_comment,
                }

相关问题 更多 >