未在终端上打印的元素

2024-10-05 11:24:36 发布

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

我正在尝试使用Scrapyhttps://www.medicregister.com/USA/list/suppliers.asp)刮取此页面。 我跟踪了每个链接,但我只得到了正确的电话。其他两个元素"Name""Email"打印不正确。对于Name它打印"None",对于"Email"它只在@符号之前打印。我认为我没有使用正确的xpath,但在Chrome中,它表明xPath实际上是我试图针对的元素。我正在使用"Anaconda Virtual Environment"。 请帮我做这个。 代码如下:

# -*- coding: utf-8 -*-
import scrapy
import logging

class Dgoodyman16Spider(scrapy.Spider):
    name = 'dgoodyman16'
    allowed_domains = ['www.medicregister.com']
    start_urls = ['https://www.medicregister.com/USA/list/suppliers.asp']

    def parse(self, response):
        all_lists = response.xpath('//a[@class="TopicHeaderSupplier"]')
        for lists in all_lists:
            title = lists.xpath('.//text()').get()
            links = lists.xpath('.//@href').get()


            yield response.follow(url=links, callback=self.parse_lists)

    def parse_lists(self, response):
        # contact name xpath: (//div[@class="vcard"]/b)[1]
        # contact phone xpath: //span[@class="tel"]
        # contact email xpath: (//noscript)[1]

        contact_name = response.xpath('(//div[@class="vcard"]/b)[1]/text()').get()
        phone = response.xpath('//span[@class="tel"]/text()').get()
        email = response.xpath('(//noscript)[1]/text()').get()


        yield {
            'Contact Name': contact_name,
            'Phone': phone,
            'Email': email
        }

enter image description here


Tags: textnameselfcomgetparseemailresponse

热门问题