import requests import time from bs4 import BeautifulSoup headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3741.400 QQBrowser/10.5.3863.400", "Cookie": "BAIDUID=E0F38C456F9E422ADF83AC42B7D6101A:FG=1; BIDUPSID=E0F38C456F9E422ADF83AC42B7D6101A; PSTM=1597193920; BD_UPN=12314753; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; SIGNIN_UC=70a2711cf1d3d9b1a82d2f87d633bd8a03473797511o04H9Bi75BJnBBaoK16ylPvPkOQLDmzuJAELo8umvVGZa%2Fc9o4GjVZu4oAREzhIccH7uo0Fi6c9RxX%2BbbFKzOg59%2BGsQ7o8FbcS%2BLzIYa1wGpvpeA9DeUQuBbu8mreIUNVBJns1DmeiZ4yvDBe3vR6s668vPp3kPA2AgoDwHlxVgIcPMiPxsuTGki84fu1hPZFYb%2BAr57UNPTwyRM%2BrLx1QdJB%2FLONPmCSEA53Ealz9QDGi8ihziF1ogN%2BDCmiJxDhCiXKQCyrhoaWFTCflscw%3D%3D93507833884706154896317941048448; BD_HOME=1; delPer=0; BD_CK_SAM=1; PSINO=6; H_PS_PSSID=1460_32532_32349_32045_32398_32115_32089_26350_32481; COOKIE_SESSION=609_0_8_8_0_16_0_1_8_3_0_2_0_0_0_0_0_0_1597388486%7C9%230_3_1597196977%7C1; H_PS_645EC=8620N7pYW4dmJpuSGiugpPHyPaDQjyd7FhOrUUV7F68eX9ZdSiDKSncHeoY" } url_list=[] r_list=[] html_list=[] url="https://www.baidu.com/s?wd={}&pn={}" sou=input("请输入需要查询的关键词:") strurl=input("请输入需要匹配的网址:") for i in range(20): url_list.append(url.format(sou,i*10)) response=requests.get(url_list[i],headers=headers) r_list.append(BeautifulSoup(response.text,"html.parser")) time.sleep(10) if r_list[i].text.find(strurl)==-1: print("没匹配",i) else: print(i) tests=r_list[i].find_all(class_="c-container") num=0 for test in tests: if test.text.find(strurl)==-1: print("不在这一行",num) else: pai = i * 10 paiming = pai + num print("关键词排名第", paiming) break num+=1 break
学了那么久的Python,今天终于搞出了一个关键词排名查询程序,适用于百度的,目前只能一个一个词查询,以后会接着完善。
评论