簡體   English   中英

Ruby-從CSV輸出中刪除HTML標簽

[英]Ruby - Remove HTML tags from CSV output

下面的代碼完美地將對話導出到收件箱中。 但是,由於收件箱是基於Web的,因此“ Message字段包含原始HTML標記;

<div>
Hello,<br><br>
I had my profile switched
</div>

我試圖在threaded_conversation.threads.last.bodycustomer_message的末尾使用strip_tags ,但無濟於事。 是否有正確的方法將此字符串保留為純文本?

CSV.open("conversations_#{mailbox_id}.csv", "wb", col_sep: ';', headers: true, force_quotes: false) do |csv|
  csv << ["Number", "From", "Subject", "Created", "Message"]
  conversations.each do |conversation|
    customer = conversation.customer
    threaded_conversation = helpscout.conversation(conversation.id)
    sleep(0.3)
    customer_message =
              begin
                body_message = threaded_conversation.threads.last.body
              rescue
                body_message = 'exception'
              end
    csv << [conversation.number, customer.email, conversation.subject, conversation.createdAt, customer_message]
  end
end

使用適當的HTML庫:

require 'nokogiri'
text = Nokogiri::HTML(body).text
# => "\nHello,\nI had my profile switched\n\n"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM