脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python+selenium实现自动化百度搜索关键词

python+selenium实现自动化百度搜索关键词

2021-07-01 00:42脚本之家 Python

在本篇文章里我们给大家分享了一篇关于python+selenium实现自动化百度搜索关键词的实例文章,需要的朋友们可以跟着操作下。

通过python配合爬虫接口利用selenium实现自动化打开chrome浏览器,进行百度关键词搜索。

1、安装python3,访问官网选择对应的版本安装即可,最新版为3.7。

python+selenium实现自动化百度搜索关键词

2、安装selenium库。

使用 pip install selenium 安装即可。

同时需要安装chromedriver,并放在python安装文件夹下,如下图所示。

python+selenium实现自动化百度搜索关键词

3、获取爬虫接口链接。

注册账号,点击爬虫代理,领取每日试用。

python+selenium实现自动化百度搜索关键词

?
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
from selenium import webdriver
 
import requests,time
 
 #自建ip池
 
def get_proxy():
 
  r = requests.get('http://127.0.0.1:5555/random')
 
  return r.text
 
import random
 
file = './tuziip.txt'
 
# 读取的txt文件路径
 
# 获取代理ip
 
def proxy_ip():
 
  ip_list = []
 
  with open(file, 'r') as f:
 
    while true:
 
      line = f.readline()
 
      if not line:
 
        break
 
      ip_list.append(line.strip())
 
  ip_port = random.choice(ip_list)
 
  return ip_port
 
def bd():
 
  chromeoptions = webdriver.chromeoptions()
 
  # 设置代理 
 
chromeoptions.add_argument("--proxy-server=http://"+proxy_ip()) 
 
# 一定要注意,=两边不能有空格,不能是这样--proxy-server = http://202.20.16.82:10152
 
  browser = webdriver.chrome(chrome_options = chromeoptions) 
 
# 查看本机ip,查看代理是否起作用 
 
  browser.get("https://www.baidu.com/"
 
  browser.find_element_by_id("kw").send_keys("ip")
 
  browser.find_element_by_id("su").click()
 
  time.sleep(2)
 
  browser.find_element_by_id("kw").clear()
 
  time.sleep(1)
 
  browser.find_element_by_id("kw").send_keys("百度")
 
  browser.find_element_by_id("su").click()
 
  time.sleep(2)
 
  browser.find_element_by_id("kw").clear()
 
  time.sleep(1)
 
  browser.find_element_by_id("kw").send_keys("百度")
 
  browser.find_element_by_id("su").click()
 
  time.sleep(2)
 
  browser.find_element_by_id("kw").clear()
 
  time.sleep(1)
 
  browser.close() 
 
# 退出,清除浏览器缓存
 
  browser.quit()
 
if __name__ == "__main__":
 
  while true:
 
    bd()

 

5、运行程序,如下图所示,可自动化搜索。

python+selenium实现自动化百度搜索关键词

延伸 · 阅读

精彩推荐