我正在尝试使用Scrapy
(https://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
}
目前没有回答
相关问题 更多 >
编程相关推荐