使用python请求在网页上搜索

时间:2019-10-03 10:06:34

标签: python web-scraping python-requests

我想获取网页的html内容。我不确定如何定义搜索字段, 我尝试了以下方法。

from fake_useragent import UserAgent
import requests


ua = UserAgent()
print(ua.chrome)
header = {'User-Agent': str(ua.chrome)}
print(header)
body = {'Search': '1.1.1.1'}
url = "https://randr.nist.gov/enzyme/Default.aspx"
htmlContent = requests.get(url, data=body) 
print(htmlContent.text)

有人可以建议如何定义正确的搜索字段吗?

1 个答案:

答案 0 :(得分:1)

可以很容易地用硒完成这样的操作:

from selenium import webdriver

search_input = '1.1.1.1'

driver = webdriver.Chrome('chromedriver.exe')
driver.get('https://randr.nist.gov/enzyme/Default.aspx')
driver.find_element_by_id('MainBody_txtSrchAutoFill').send_keys(search_input)
driver.find_element_by_id('MainBody_ImgSrch').click()
result_table = driver.find_element_by_id('MainBody_gvSearch')
print(result_table.text)