Scrapy
在控制台/终端中运行时显示许多信息,您应该检查消息中的内容,因为我收到了错误消息
File "<pyshell#0>", line 15, in parse
name = box.response.xpath('.//div[@class="col-11"]//p//text()').extract_first()
AttributeError: 'NoneType' object has no attribute 'xpath'
这表明这是必须的
box.xpath
而不是
box.response.xpath
最小工作代码。
它给了我文件
CSV
共有87项。
您可以将所有代码放在一个文件中并运行
python script.py
不创造
project
import scrapy
class XubioContadoresSpider(scrapy.Spider):
name = "XubioContadores"
start_urls = [
f'https://socios.xubio.com/ar/contadores/?pag={i}' for i in range(1, 10)
]
def parse(self, response):
print('url:', response.url)
for box in response.xpath('//div[@class="w-100 padding-15 "]'):
name = box.xpath('.//div[@class="col-11"]//p//text()').extract_first()
phone = box.xpath('.//div[@class="col-md-2 col-12 h-60"]//p//text()').extract_first()
address = box.xpath('.//div[@class="col-md-4 col-12 h-60"]//p//text()').extract_first()
email = box.xpath('.//div[@class="col-md-3 col-12 h-60"]//p//text()').extract_first()
item = dict()
item['name'] = name
item['phone'] = phone
item['address'] = address
item['email'] = email
yield item
# --- run without project and save in `output.csv` ---
from scrapy.crawler import CrawlerProcess
c = CrawlerProcess({
'USER_AGENT': 'Mozilla/5.0',
'FEEDS': {'output.csv': {'format': 'csv'}}, # new in 2.1
})
c.crawl(XubioContadoresSpider)
c.start()