我目前正在尝试从 alibaba.com 抓取公司概述。
为了获得像公司名称这样的信息,我做了:
response.xpath("//a[@class='company-name company-name-lite-vb']/text()").extract()
哪个工作正常。
当输入“公司概览”>“公司简介”并尝试从表中抓取信息时:
response.xpath("//div/div[@class='content-value']").extract()
我得到一个空数组。
资源/search_results_searchpage.yml:
products:
css: 'div[data-content="productItem"]'
multiple: true
type: Text
children:
link:
css: a.elements-title-normal
type: Link
爬虫.py:
import scrapy
import csv
#from scrapy_selenium import SeleniumRequest # only needed when using selenium
import os
from selectorlib import Extractor
class Spider(scrapy.Spider):
name = 'alibaba_crawler'
allowed_domains = ['alibaba.com']
start_urls = ['http://alibaba.com/']
link_extractor = Extractor.from_yaml_file(os.path.join(os.path.dirname(__file__), "../resources/search_results_searchpage.yml"))
def start_requests(self):
search_text="Headphones"
url="https://www.alibaba.com/trade/search?fsb=y&IndexArea=product_en&CatId=&SearchText={0}&viewtype=G".format(search_text)
yield scrapy.Request(url, callback = self.parse, meta = {"search_text": search_text})
def parse(self, response):
data = self.link_extractor.extract(response.text, base_url=response.url)
for product in data['products']:
parsed_url=product["link"]
yield scrapy.Request(parsed_url, callback=self.crawl_mainpage)
#yield SeleniumRequest(url=parsed_url, callback=self.crawl_mainpage)
def crawl_mainpage(self, response):
yield {
'name': response.xpath("//h1[@class='module-pdp-title']/text()").extract(),
'Year of Establishment': response.xpath("//td[contains(text(), 'Year Established')]/following-sibling::td/div/div/div/text()").extract()
}
有人知道我可以做些什么来填充 Est. 年吗?我尝试使用 scrapy_selenium 并正确配置它,因为我怀疑该对象是动态生成的但仍然没有运气,或者我可能使用错误
与:
scrapy crawl alibaba_crawler -o out.csv -t csv