-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
executable file
·51 lines (41 loc) · 1.59 KB
/
main.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
from selenium import webdriver
from selenium.webdriver.edge.service import Service as EdgeService
from selenium.webdriver.edge.options import Options as EdgeOptions
from selenium.webdriver.common.by import By
import time
import random
from tqdm import tqdm
def init_browser(keyword):
edge_options = EdgeOptions()
service = EdgeService(executable_path="./msedgedriver.exe")
driver = webdriver.Edge(service=service, options=edge_options)
try:
driver.get("https://cn.bing.com")
time.sleep(random.randint(2, 10))
search_box = driver.find_element(By.NAME, "q")
time.sleep(random.randint(2, 10))
search_box.send_keys(keyword)
search_box.submit()
time.sleep(random.randint(2, 10))
except Exception as e:
print(e)
return driver
def bing_search(driver, keyword):
try:
element = driver.find_element(By.XPATH, '//*[@id="sb_form_q"]')
element.clear()
time.sleep(random.randint(2, 4))
element.send_keys(keyword)
time.sleep(random.randint(1, 3))
element.submit()
time.sleep(random.randint(3, 5))
except Exception as e:
print(e)
if __name__ == "__main__":
with open(r'关键词.txt','r') as file:
keyword_list = [i.split('\n')[0] for i in file.readlines()]
keyword = random.choice(keyword_list)
edge_driver = init_browser(keyword)
for i in tqdm(range(1000), desc="bing searches", unit="search"):
bing_search(edge_driver,random.choice(keyword_list))
time.sleep(random.randint(3, 5))