-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmsearch.go
118 lines (104 loc) · 2.68 KB
/
tmsearch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"encoding/csv"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/antchfx/htmlquery"
"github.com/urfave/cli"
"golang.org/x/net/html"
)
func main() {
var tmsearchURL string
var numResults int
app := &cli.App{
Name: "tmsearch",
Usage: "Scrape records from the tmsearch database for particular search terms",
Action: func(c *cli.Context) error {
scrape(tmsearchURL, numResults)
return nil
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "tmsearchURL",
Value: "<unset>",
Usage: "The root url for scraping results",
Destination: &tmsearchURL,
},
&cli.IntFlag{
Name: "numResults",
Value: 1,
Usage: "The root url for scraping results",
Destination: &numResults,
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
// Results from a freeform search on http://tmsearch.uspto.gov/ for
// (neon$)[FM] and (live)[LD] and (009)[CC]
func scrape(tmsearchURL string, numResults int) {
client := &http.Client{
CheckRedirect: http.DefaultClient.CheckRedirect,
}
csvWriter := csv.NewWriter(os.Stdout)
for i := 1; i <= numResults; i++ {
url := tmsearchURL + strconv.Itoa(i)
req := newRequest(url)
log.Println("Scraping results from url", url)
resp, err := client.Do(req)
if err != nil {
log.Fatal("Error reading response", err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
bodyString := string(bodyBytes)
processResult(i, csvWriter, bodyString)
}
time.Sleep(2 * time.Second)
}
}
func processResult(resultNum int, csvWriter *csv.Writer, body string) {
doc, err := htmlquery.Parse(strings.NewReader(body))
if err != nil {
log.Fatal("Unable to parse html doc", err)
return
}
writeLine(csvWriter, doc, htmlquery.Find(doc, "//table[4]/tbody/tr/td[1]/b"))
writeLine(csvWriter, doc, htmlquery.Find(doc, "//table[4]/tbody/tr/td[2]"))
csvWriter.Flush()
}
func writeLine(csvWriter *csv.Writer, doc *html.Node, nodes []*html.Node) {
cols := make([]string, 0)
for _, node := range nodes {
cols = append(cols, htmlquery.InnerText(node))
}
csvWriter.Write(cols)
}
func newRequest(url string) *http.Request {
content, err := ioutil.ReadFile("cookie.txt")
if err != nil {
log.Fatal("Error reading cookie file", err)
}
req, err := http.NewRequest("GET", url, nil)
cookies := strings.Split(string(content), ";")
for _, cookie := range cookies {
keyValue := strings.Split(cookie, "=")
req.AddCookie(&http.Cookie{
Name: strings.TrimSpace(keyValue[0]),
Value: strings.TrimSpace(keyValue[1]),
})
}
return req
}