Advertisement
here2share

# webscrape2autocomplete.py

Dec 18th, 2022
1,015
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.42 KB | None | 0 0
  1. # webscrape2autocomplete.py
  2.  
  3. import requests
  4. from Tkinter import *
  5.  
  6. def fetch_titles():
  7.     # Fetch the HTML page using the requests module
  8.     page = requests.get("https://docs.python.org/3/library/index.html")
  9.     page_html = page.text
  10.    
  11.     # Use regular expressions to extract the titles from the HTML page
  12.     import re
  13.     titles = re.findall(r'<a class="reference internal" .*?>(.*?)</a>', page_html)
  14.    
  15.     # Return the list of titles
  16.     return titles
  17.  
  18. def autocomplete(event):
  19.     # Get the current value of the input field
  20.     value = event.widget.get()
  21.    
  22.     # Use the fetch_titles function to get a list of titles
  23.     titles = fetch_titles()
  24.    
  25.     # Filter the list of titles based on the current value of the input field
  26.     matches = [title for title in titles if title.startswith(value)]
  27.    
  28.     # Set the choices for the autocomplete list
  29.     autocomplete_list.set_completions(matches)
  30.    
  31. # Create the root window
  32. root = Tk()
  33.  
  34. # Create an input field and a label
  35. input_field = Entry(root)
  36. label = Label(root, text="Enter a keyword:")
  37.  
  38. # Create an autocomplete list
  39. autocomplete_list = Autocomplete(root)
  40.  
  41. # Bind the autocomplete function to the <KeyRelease> event of the input field
  42. input_field.bind("<KeyRelease>", autocomplete)
  43.  
  44. # Place the widgets in the root window
  45. input_field.pack()
  46. label.pack()
  47. autocomplete_list.pack()
  48.  
  49. # Run the Tkinter event loop
  50. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement