我一直在写一个搜索查询工具,但在一系列搜索之后却一直出现这个错误

2024-04-19 15:46:54 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,当main()在main中输入两个输入后调用函数Dorker(query,country)时,我的函数Dorker(query,country)没有执行,它应该返回打印出根据查询和国家找到的所有URL,然而,它所做的是在整整一分钟内不返回任何内容,然后给我一个Error code 429并阻塞我的IP。我尝试过改变参数中的变量,以使其正确通过,但没有成功。当输入不在任何函数中且if语句与它在同一块中时,函数Dorker(query,country)工作正常

import re
import urllib.parse
import urllib.request
import time
import os



#sets global variable 
win  = 'Windows'
unix = 'Linux'
if os.name == 'nt':
 OS = 'Windows'
else:
 OS = 'Linux'

#clears the screen pretty self explanatory
def clear():
 global newprompt
 newprompt = True

 if OS == unix:
     os.system('clear')          
     main()
 else:
     os.system('cls')
     main()

#returns as a error in input
def invalidcommand():
 print("\033[0;31mInvalid \033[1;31mCommand")
 time.sleep(.2)
 clear()
 main()

#creates a txt file to store the urls
def urlwrite():
 with open('url.txt', 'w') as f:
     f.close()
urlwrite()

#Sends requests to google in order to find urls based on query
def dorker(query,country):
 payload = (query +'&num=100&cr=country'+country)
 r = requests.get('https://www.google.com/search?q=inurl:' + payload)
 if r.status_code == 200:
     pattern = r'<div class="kCrYT"><a href="\/url\?q=(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})*">'
     url_list = re.findall(pattern, r.text)
 else:
     print("Error! Code: <" + str(r.status_code) + ">")
    
 if len(dorker(query,country)) != 0:
     url_list = dorker(query,country)
     how_much = 0
     for i in range(0, len(url_list)):
         how_much += 1
         currentUrl = urllib.parse.urlparse(urllib.parse.unquote(url_list[i])).netloc
         urlWithQuery = currentUrl + '/' + urllib.parse.unquote(query)
         with open('url.txt', 'a') as f:
             try:
                 realUrl = urllib.request.urlopen(url_list[i]).geturl()
             except:
                 realUrl = urlWithQuery
                 pass
             print(currentUrl + " --> " + realUrl)
             f.write(realUrl + '\n')
         print("=== WE DONE, [" + str(how_much) + "] ===")
 else:
         print("=== NOTHING FOUND?! ===")
#Main function in order to call the other functions
def main():
 maininput = input("Select your option:")
 if maininput.lower() == "a":
     try: 
         query = urllib.parse.quote(input("> Dork: "))
         country  = input(">Country: ")
         dorker(query, country)
     except ValueError:
         invalidcommand()
 else:
     invalidcommand()





if __name__ == "__main__":
 time.sleep (1)  
 clear()
 main()
while True:
 try:    
     main()
 except KeyboardInterrupt:
     print('')
     pass   ```