Fix trends
This commit is contained in:
parent
2f2b073de7
commit
7f033acb89
1 changed files with 17 additions and 9 deletions
26
trends.go
26
trends.go
|
|
@ -1,33 +1,41 @@
|
|||
package twitterscraper
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
)
|
||||
|
||||
const trendsURL = "https://twitter.com/i/trends"
|
||||
const trendsURL = "https://mobile.twitter.com/trends"
|
||||
|
||||
// GetTrends return list of trends.
|
||||
func GetTrends() ([]string, error) {
|
||||
req, err := newRequest(trendsURL)
|
||||
req, err := http.NewRequest("GET", trendsURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Accept-Language", "en-US")
|
||||
|
||||
htm, err := getHTMLFromJSON(req, "module_html")
|
||||
if err != nil {
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if resp == nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
doc, err := goquery.NewDocumentFromReader(htm)
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("response status: %s", resp.Status)
|
||||
}
|
||||
|
||||
doc, err := goquery.NewDocumentFromReader(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var trends []string
|
||||
doc.Find("li").Each(func(i int, s *goquery.Selection) {
|
||||
if trend, ok := s.Attr("data-trend-name"); ok {
|
||||
trends = append(trends, trend)
|
||||
}
|
||||
doc.Find("li.topic").Each(func(i int, s *goquery.Selection) {
|
||||
trends = append(trends, strings.TrimSpace(s.Text()))
|
||||
})
|
||||
return trends, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue