【发布时间】:2021-08-25 07:04:52
【问题描述】:
我创建了一个scrapy web scraper,但我不知道如何为这个web scraper 创建测试用例和数据点验证器。
我有三个相互关联的解析器:
def parse(self, response):
urls = response.xpath('').extract()
for url in urls:
yield scrapy.Request(url, callback=self.parse_company_index)
def parse_company_index(self, response):
print("procesing:"+response.url)
name = response.xpath('').extract()
urls = response.xpath('').extract()
data = zip(name, urls)
for item in data:
dict = {
'record_type': 'company_index',
'company_name': item[0],
'source_url': item[1],
}
yield dict
for url in urls:
yield scrapy.Request(url, callback=self.parse_company_profiles)
next_page = response.xpath('').get()
if next_page is not None:
yield scrapy.Request(next_page, callback=self.parse_company_index)
def parse_company_profiles(self, response):
Company_name = response.xpath('').extract()
Company_location = response.xpath('').extract()
Company_website = response.xpath('').extract()
Company_webdomain = response.xpath('').extract()
Company_industry = response.xpath('').extract()
Company_employee_size = response.xpath('').extract()
Company_revenue = response.xpath('').extract()
Contact_name = response.xpath('').extract()
Contact_jobtitle = response.xpath('').extract()
Contact_email_domain = response.xpath('').extract()
Contact_detail = []
if Contact_name:
for i in range(len(Contact_name)):
Contact_detail.append({'Contact_name': Contact_name[i],
'Contact_jobtitle': Contact_jobtitle[i],
'Contact_email_domain': Contact_email_domain[i]})
Contact_details = [Contact_detail]
else:
Contact_details = ["None"]
data = zip(Company_name, Company_location, Company_website, Company_webdomain, Company_industry,
Company_employee_size, Company_revenue, Contact_details)
for item in data:
dict = {
'record_type': 'company_profiles',
'company_name': item[0],
'company_location': item[1],
'company_website': item[2],
'company_webdomain': item[3],
'company_industry': item[4],
'company_employee_size': item[5],
'company_revenue': item[6],
'contact_details': item[7]
}
yield dict
我最初认为这意味着创建一个单元测试,但现在我考虑它,它看起来不像。另外,如果是单元测试,我应该采取什么方法来解决这个问题?
任何帮助将不胜感激。
【问题讨论】:
标签: python unit-testing validation testing scrapy