Web scraping 如何从scrapy中替换或删除特殊字符?
我刚开始学习scrapy,试图让spider从网站上抓取一些信息,并尝试替换或删除短描述中的特殊字符Web scraping 如何从scrapy中替换或删除特殊字符?,web-scraping,scrapy,special-characters,Web Scraping,Scrapy,Special Characters,我刚开始学习scrapy,试图让spider从网站上抓取一些信息,并尝试替换或删除短描述中的特殊字符 import scrapy class TravelspudSpider(scrapy.Spider): name = 'travelSpud' allowed_domains = ['www.tripadvisor.ca'] start_urls = [ 'https://www.tripadvisor.ca/Attractions-g294265-
import scrapy
class TravelspudSpider(scrapy.Spider):
name = 'travelSpud'
allowed_domains = ['www.tripadvisor.ca']
start_urls = [
'https://www.tripadvisor.ca/Attractions-g294265-Activities-c57-Singapore.html/'
]
base_url = 'https://www.tripadvisor.ca'
def parse(self, response, **kwargs):
for items in response.xpath('//div[@class= "_19L437XW _1qhi5DVB CO7bjfl5"]'):
yield {
'name': items.xpath('.//span/div[@class= "_1gpq3zsA _1zP41Z7X"]/text()').extract()[1],
'reviews': items.xpath('.//span[@class= "DrjyGw-P _26S7gyB4 _14_buatE _1dimhEoy"]/text()').extract(),
'rating': items.xpath('.//a/div[@class= "zTTYS8QR"]/svg/@title').extract(),
'short_descr': items.xpath('.//div[@class= "_3W_31Rvp _1nUIPWja _17LAEUXp _2b3s5IMB"]'
'/div[@class="DrjyGw-P _26S7gyB4 _3SccQt-T"]/text()').extract(),
'place': items.xpath('.//div[@class= "ZtPwio2G"]'
'/div'
'/div[@class= "DrjyGw-P _26S7gyB4 _3SccQt-T"]/text()').extract(),
'cost': items.xpath('.//div[@class= "DrjyGw-P _26S7gyB4 _3SccQt-T"]'
'/div[@class= "DrjyGw-P _1SRa-qNz _2AAjjcx8"]'
'/text()').extract(),
}
next_page_partial_url = response.css("div._1I73Kb0a").css("div._3djM0GaD").xpath('.//a/@href').extract_first()
if next_page_partial_url is not None:
next_page_url = self.base_url + next_page_partial_url
yield scrapy.Request(next_page_url, callback=self.parse)
我试图替换的角色是徒步旅行路线•风景步行区
。CSV文件中的点解码为<代码> > < <代码> >
其他一切都像一种魅力
我尝试使用.replace()
,但出现错误:
AttributeError: 'list' object has no attribute 'replace'
如果您删除这些特殊字符仅仅是因为它们在CSV文件中的显示很奇怪,那么我建议您不要删除它们,我们将不胜感激。只需在
settings.py
文件中添加以下行即可
FEED_EXPORT_ENCODING = 'utf-8-sig'
这将打印CSV文件中的特殊字符。如果您删除这些特殊字符只是因为它们在CSV文件中的显示很奇怪,那么我建议不要删除它们。只需在
settings.py
文件中添加以下行即可
FEED_EXPORT_ENCODING = 'utf-8-sig'
这将打印CSV文件中的特殊字符。extract()方法返回列表。尝试先使用extract_()然后使用replace方法。类似于这样的内容。首先提取()。替换(…)@sagunsrestha,谢谢!我将尝试使用itextract()方法返回列表。尝试先使用extract_()然后使用replace方法。类似于这样的内容。首先提取()。替换(…)@sagunsrestha,谢谢!我将尝试它伟大的解决方案,谢谢!现在一切都很好很好的解决方案,谢谢!现在一切都好了