Use python selenium WEB automation library to realize automatic click ranking when opening Baidu search in Chrome browser

Implementation principle:
1. Instantiate the browser and open the Baidu homepage
2. Enter your own website keywords and search
3. Check whether your own website is included in the search results, click if so, and exit with a 10-second delay,
4. If not, turn the page and continue checking in a loop

I just made some rough code demonstrations and made some rough parameter restrictions. You can modify them according to your own situation. And there is no proxy function added. Avoid using it for random purposes.
Given the code:

Need to install selenium library

pip install selenium

Chrome needs to be installed manually
https://www.google.cn/chrome/

You need to manually download the corresponding browser driver webdriver (generally you do not need to worry about the small version, the large version can correspond to or a similar version, it is recommended to use the latest one directly)

http://npm.taobao.org/mirrors/chromedriver/The
version I use here is placed in the root directory of the Python running environment after downloading.
Insert image description here
This is the project root directory, so it is placed directly with the main program. Generally, if you report an error, it means there is something wrong with the path where the driver is placed. please confirm.
Insert image description here

from selenium import webdriver
from time import sleep
import random

while True:
    if random.randint(1,2)==1:
        keyword = "寻无极办公软件机器人3.0"
    else:
        keyword = "企业微信办公客服机器人定制"
    print(keyword)
    for i in range(2, len(keyword) + 1):
        wd=keyword[:i+1]
        print(str(i)+wd)
        chrome = webdriver.Chrome("chromedriver.exe")
        chrome.implicitly_wait("20")
        chrome.get("https://baidu.com")
        chrome.find_element_by_css_selector("#kw").send_keys(wd+"\n")


        def s_click(b):
            sleep(random.randint(4,30))
            try:
                arr = b.find_elements_by_css_selector("div#content_left > div ")
                for a in arr:
                    print(a.text)
                    if a.text.find("23xiu.cn") != -1:#定义了点击哪个网站
                        a.find_element_by_css_selector("h3.t").click()
                        return 1

            except:
                print("可能没有窗口之类的,发生了一些错误")
                return -1

        s_click(chrome)
        n = 0

        while True:
            arr_p = chrome.find_elements_by_css_selector("#page >div.page-inner >a")

            n = n + 1
            for p in arr_p:
                print(p.text)

                if p.text.find("下一页") != -1:
                    p.click()
                    if s_click(chrome)==1:
                        break
            if n > 10:
                break

        chrome.close()
    sleep(60*60*1)


I searched here for a few keywords that I want to automatically click on. The implementation method is very simple. Novices can copy the code and run it first, and then modify it to their own keywords. Don't change it directly after you get it. Something went wrong and I don’t know where the problem is.

If you have any questions, please contact me on WeChat: huang582716403

Guess you like

Origin blog.csdn.net/ZhiMaoYiDeHuaiRen/article/details/118079762