from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Chrome()
browser.get('https://en.wikipedia.org/wiki/Main_Page')
print("Enter a keyword to search on wikipedia: ", end='')
keyword = input()
elem = browser.find_element_by_id('searchInput') # Find the search box
elem.send_keys(keyword + Keys.RETURN)
# do something with the opened page
browser.quit()
如果模拟浏览器用户活动是可以的,您可以考虑安装Selenium和Chrome webdriver(下面是说明:https://pypi.org/project/selenium/)。 “例1”与问题的解决方案类似。在
搜索栏是
<input type="search" name="search" placeholder="Search Wikipedia" title="Search Wikipedia [alt-shift-f]" accesskey="f" id="searchInput" tabindex="1" autocomplete="off">
元素,有“searchInput”id,您可以使用这个id用el = browser.find_element_by_id("searchInput")
选择它 {3>搜索并使用cd3}填充请求。在因此,脚本可能如下所示:
如果您不想分析浏览器活动,您可以使用
requests
和BeautifulSoup4
模块来解决它,但是解决方案会更复杂,尽管可能更高效相关问题 更多 >
编程相关推荐