doukang1962 2018-12-07 21:20
浏览 62
已采纳

如何删除仅从特定单词的最后一个实例开始的整个字符串?

I am trying to scrape some data from an RSS link. I am just beginning this project; there will be some stuff with GUIs later. I am having trouble removing some stuff that I don't want showing on a particular line. In this case, I want everything after the last "at" gone, so that it only shows the Job Title.

I have tried replacing instances of the "at" string with an empty string, but that would also remove any instance of "a" followed by "t" from the string. I think I have to set up a map of words separated by white spaces (so strings.Fields() maybe?), and then set up a for loop that will replace the entire string starting from a word.

Code:

package main

import (
    "encoding/xml"
    "fmt"
    "log"
    "net/http"
    "strings"
)

type JobInfo struct{
    Title string `xml:"title"`
    Location string `xml:"location"`
    Company string `xml:"a10:author"`
    PostDate string `xml:"pubDate"`
    Description string `xml:"description"`
}

type Channel struct{
    Title string `xml:"title"`
    Link  string `xml:"link"`
    Desc  string `xml:"description"`
    Items []JobInfo `xml:"item"`
}

type Rss struct {
    Channel Channel `xml:"channel"`
} 

func main() {
    resp, err := http.Get("https://stackoverflow.com/jobs/feed?l=Bridgewater%2c+MA%2c+USA&u=Miles&d=100")
    if err != nil{
        log.Fatal(err)
        return
    }
    defer resp.Body.Close()

    rss := Rss{}

    decoder := xml.NewDecoder(resp.Body)
    err = decoder.Decode(&rss)
    if err != nil{
        log.Fatal(err)
        return
    }

    fmt.Printf("%v
", rss.Channel.Title)
    for i, item := range rss.Channel.Items{
        fmt.Printf("%v. Job Information:
", i+1)
        fmt.Printf("Title: %v
", item.Title)
        fmt.Printf("Location: %v
", item.Location)
        fmt.Printf("Company: %v
", item.Company)
        postdate := strings.Replace(item.PostDate, "Z", "", -1)
        fmt.Printf("Post Date: %v
", postdate)
        fmt.Printf("Description: %v
", item.Description)
    }
}

Output (1 example):

1. Job Information:
Title: Senior Web Engineer at Maark (Boston, MA)
Location: Boston, MA
Company: 
Post Date: Fri, 07 Dec 2018 20:21:34 
Description: <p>At MAARK, we are passionate about bringing innovation and advanced concepts to life and sweating every detail in order to create the best possible experience for our clients and their customers. We believe that every interaction and transaction a user has with a product should be designed and built to work for that individual.</p><p>The clients we work with span a host of verticals  ranging from large financial institutions to telecoms to hospitality to bio-engineering. And the problems we solve for them range from highly animated, interactive frontends, to complex business rules on the backend, to IoT solutions that bridge digital and real worlds. We love diving into new subjects and researching our clients' businesses and look to work with people who are just as naturally curious.<br></p><p>In this role, you will work as part of our growing development team to craft and build frontend side of apps and web sites. We work on highly creative projects, utilize a wide variety of fullstack technologies, and empower each of our developers to create innovative solutions and explore and master emerging web technologies.</p><p>We are looking for local (Boston metro area) candidates only. No remote opportunities for this position. </p><br><p><strong>Benefits</strong></p><br><p><strong>About Maark</strong><br></p><p>Maark is a strategic marketing and innovation agency for global companies - headquartered in Boston, MA. We help our clients define and articulate their vision, design new connected customer experiences, and develop applications at the intersection of where whats possible meets whats relevant. </p><p>We are proud to foster a workplace free from discrimination. We strongly believe that diversity of experience, perspectives, and background will lead to a better environment for our employees and a better experience for our clients.</p><p><em>Maark is an Equal Employment Opportunity (EEO) employer. It is the policy of Maark to prohibit discrimination and harassment of any type and to afford equal employment opportunities to all persons without regard to race, color, religion, sex, national origin, age, gender, physical or mental disability, veteran-status, or any other characteristic protected by applicable federal, state or local law.</em></p>

In the Title field, this should only say: Senior Web Engineer

I will figure out the company name and the bytestring in the description later, but if anyone has any input on that, it would be appreciated!

  • 写回答

1条回答 默认 最新

  • doulandai0641 2018-12-07 22:10
    关注

    hope this works for you

       fmt.Println(item.Title[:strings.LastIndex(item.Title," at ")])
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀