python - 有什么方法可以翻译网页语言,或者在使用scrapy抓取的同时翻译抓取的数据?
问题描述
我打算用英语抓取 dintex.net 网站,但找不到任何方法将抓取的数据转换为英语。我也使用了googletans,但它也显示错误,那么有没有其他方法可以将该页面或数据转换为英文?
import scrapy
from googletrans import Translator
class DtSpider(scrapy.Spider):
name = 'dt'
start_urls = ['http://www.dintex.net']
def parse(self, response):
urls = response.xpath('//*[@class="listing-btn btn btn-primary btn-block w-100"]/@href').extract()
for url in urls:
url = response.urljoin(url)
yield scrapy.Request(url=url, callback=self.parse_details)
np = response.xpath('//*[@class="page-item"]/a[@rel="next"]/@href').extract_first()
ap = response.urljoin(np)
yield scrapy.Request(url=ap,callback=self.parse)
def parse_details(self,response):
Title = response.xpath('//*[@class="show-info__title"]/text()').extract_first()
Location = response.xpath('//*[@class="show-info__location"]/p/text()').extract_first()
Contact = response.xpath('//*[@class="show-info__contact-details__phone-link"]/text()').extract_first()
Contact = Contact.replace('Whatsapp ','')
Description = response.xpath('//*[@class="show-info__section-text"]/p/text()').extract_first()
Manufacture = response.xpath('//td[contains(text(),"Fabricante")]/following-sibling::td/text()').extract_first()
Model = response.xpath('//td[contains(text(),"Modelo")]/following-sibling::td/text()').extract_first()
Year = response.xpath('//td[contains(text(),"Año")]/following-sibling::td/text()').extract_first()
Condition = response.xpath('//td[contains(text(),"Condición")]/following-sibling::td/text()').extract_first()
img = response.xpath('//*[@class="gallery__item"]/img/@src').extract_first()
thumbs = response.xpath('//img/@lazy-src').extract()
#t = Translator()
#Title = t.translate(Title).text
#Location = t.translate(Location).text
#Contact = t.translate(Contact).text
#Description = t.translate(Description).text
#Manufacture = t.translate(Manufacture).text
#Model = t.translate(Model).text
#Year = t.translate(Year).text
#Condition = t.translate(Condition).text
yield{'Title': Title,
'Location' : Location,
'Contact' : Contact,
'Description' : Description,
'Manufacture' : Manufacture,
'Model' : Model,
'Year' : Year,
'Condition' : Condition,
'Img' : img,
'Thums' : thumbs
}
解决方案
我认为你应该在你的请求中发送这个 cookie
googtrans=/es/en
由于页面允许根据可用语言/区域的选择进行本地化。
你需要做这样的事情,看看来自scrapy docs的scrapy请求的cookie部分
您产生的请求可能需要更改这样的内容(未测试)
scrapy.Request(url=url, cookies= {'googletrans': '/es/en'}, callback=self.parse_details)
推荐阅读
- c# - C# 控制台应用程序中未显示货币符号
- postgresql - 错误:类型时间戳的输入语法无效
- json - SQL Server JSON 操作
- android - 在我为 ViewPageAdapter 添加一些代码后,我的应用程序总是在启动时强制关闭
- spring-boot - 在模态表创建期间忽略字段的创建,但通过在 Jpa 接口中调用本机 sql 过程而不是 @Transient 来获取数据
- javascript - Node JS 中的 socket.makefile() 等价物
- python - 在python中,如何解码文字内容为utf-8的字符串?
- string - 制作两个字符串 Anagram 和 2 个字符串的最小步骤数长度相同且只有小写字母
- c - read() 是否在 Linux 中等待?
- restfb - 阅读页面消息