-
Notifications
You must be signed in to change notification settings - Fork 0
/
Google.py
36 lines (35 loc) · 1.2 KB
/
Google.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
import requests
import re
from bs4 import BeautifulSoup
class Google:
fname= open("links.txt" , "a+" , encoding = "utf8")
def __init__(self,parameter):
self.params= parameter
def results(self,page = 0):
self.pages= page
self.url = f'https://www.google.com/search?nfpr=1&q={self.params}&start={self.pages}'
self.headers = {
"user-agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML\
, like Gecko) Chrome/81.0.4044.138 Safari/537.36"
}
rq = requests.get(self.url, headers = self.headers)
soup = BeautifulSoup(rq.content , "html5lib")
box = soup.find(id = "search")
links = box.find_all('div' , class_ = 'r')
for i in links :
res = i.find(attrs = {'href':re.compile('http')})
print(res['href'] ,file = Google.fname)
rng= input('How many pages would you like to parse?\n')
if isinstance(rng, int):
pass
else:
print("Please enter an integer!\n")
exit()
param = input("Enter search phrase:\n")
param.replace(' ' , '+')
pg = 0
google = Google(param )
if int(rng):
for i in range(rng):
pg = i*10
google.results(pg)