Как удалить теги HTML и '\ n' из вывода Scrapy Python - PullRequest
0 голосов
/ 25 мая 2019

Я новичок в Python и Web Scraping. Я написал ниже 2 строки, чтобы извлечь заголовок и цену с сайта. Однако он выводит теги html и символы \ n. Как я могу удалить их и получить только текстовый вывод?

product_name = response.css('#productTitle::text')[0].extract().strip('\n')
product_price = response.css('#priceblock_ourprice')[0].extract().strip()

Выход

[
    "                \n                    \n                    \n                \n\n                \n                    \n                    \n                        Stainless Steel Food Grinder Attachment fit KitchenAid Stand Mixers Including Sausage Stuffer, Dishwasher Safe,Durable Mixer Accessories as Meat Processor\n                    \n                \n\n                \n                    \n                    \n                \n            ",
    "<span id=\"priceblock_ourprice\" class=\"a-size-medium a-color-price priceBlockBuyingPriceString\">$87.99</span>"
]

1 Ответ

1 голос
/ 25 мая 2019

Удаление лишних пробелов \n:

for text in str_list:
    text = text.replace("\n","") #remove all '\n' from text
    while "  " in text : # if 2 space symbols in sting
        r_str = text .replace("  ", " ") # replace 2 spaces with 1 space and repeat until no more 2 consecutive spaces in text

Второй селектор также должен иметь ::text в селекторе:
product_price = response.css('#priceblock_ourprice::text').extract_first()

...