Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # webscrape2autocomplete.py
- import requests
- from Tkinter import *
- def fetch_titles():
- # Fetch the HTML page using the requests module
- page = requests.get("https://docs.python.org/3/library/index.html")
- page_html = page.text
- # Use regular expressions to extract the titles from the HTML page
- import re
- titles = re.findall(r'<a class="reference internal" .*?>(.*?)</a>', page_html)
- # Return the list of titles
- return titles
- def autocomplete(event):
- # Get the current value of the input field
- value = event.widget.get()
- # Use the fetch_titles function to get a list of titles
- titles = fetch_titles()
- # Filter the list of titles based on the current value of the input field
- matches = [title for title in titles if title.startswith(value)]
- # Set the choices for the autocomplete list
- autocomplete_list.set_completions(matches)
- # Create the root window
- root = Tk()
- # Create an input field and a label
- input_field = Entry(root)
- label = Label(root, text="Enter a keyword:")
- # Create an autocomplete list
- autocomplete_list = Autocomplete(root)
- # Bind the autocomplete function to the <KeyRelease> event of the input field
- input_field.bind("<KeyRelease>", autocomplete)
- # Place the widgets in the root window
- input_field.pack()
- label.pack()
- autocomplete_list.pack()
- # Run the Tkinter event loop
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement