-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoogleSpider.py
66 lines (62 loc) · 2.7 KB
/
GoogleSpider.py
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
import time
from pprint import pprint
import requests
from bs4 import BeautifulSoup
class GoogleSpider:
def __init__(self, **kwargs):
self.keyword = kwargs.get("keyword")
def __del__(self):
pass
def search(self, **kwargs) -> list:
data = []
if kwargs.get("keyword") is None:
if self.keyword is None:
return []
else:
query = self.keyword
else:
query = kwargs.get("keyword")
query = query.replace(' ', '+')
URL = f"http://google.com/search?q={query}"
page = 1
while True:
try:
print("当前正在搜索【" + str(query) + "】,当前第" + str(page) + "页...")
USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:65.0) Gecko/20100101 Firefox/65.0"
headers = {"user-agent": USER_AGENT}
resp = requests.get(URL, headers=headers, verify=True)
if resp.status_code == 200:
soup = BeautifulSoup(resp.content, "html.parser")
nextpage = soup.find_all("a", id="pnnext")
if len(nextpage) > 0 and 'href' in nextpage[0].attrs:
nextpage = str(nextpage[0]['href'])
URL = r"http://www.google.com" + nextpage
results = []
for g in soup.find_all('div', class_='g'):
anchors = g.find_all('a')
if anchors:
if 'href' in anchors[0].attrs:
link = anchors[0]['href']
title_list = list(g.find_all('h3'))
if len(title_list) > 0:
title_str = title_list[0]
title_soup = BeautifulSoup(str(title_str), 'html.parser')
title_text = title_soup.get_text()
title_soup.clear()
item = {
"title": title_text,
"link": link
}
results.append(item)
data.append({
page: results
})
page += 1
resp.close()
time.sleep(1)
else:
break
except Exception as e:
pprint(e)
break
return data