2017-04-22 40 views
-1

我想从名为myip.ms的网站检索数据。我正在使用请求发送数据,然后我希望将响应页面发回给我。当我运行脚本时,它会返回相同的页面(主页)作为响应。我想要使​​用我提供的查询的下一页。我是WebScrapping中的新成员。这是我用来实现这个的代码。 导入请求 从进口的urllib.parse用urlencode,quote_plus 有效载荷= { '名称': 'educationmaza.com', '值': 'educationmaza.com', } 有效载荷=用urlencode(有效载荷) R = requests.post( “http://myip.ms/s.php”,数据有效载荷=) 的infile =打开( “E://abc.html”, 'WB') infile.write(r.content) infile.close()如何在网站中使用python requests模块进行搜索并返回响应页面?

回答

0

我不是专家,但看起来在与网页互动时,该帖子是由jQuery处理的,而requests并不适合。因此,您将不得不使用Selenium模块与其交互。 根据需要将下面的代码执行:

from selenium import webdriver 
driver = webdriver.PhantomJS() 

driver.get("https://myip.ms/s.php") 
driver.find_element_by_id("home_txt").send_keys('educationmaza.com') 
driver.find_element_by_id("home_submit").click() 

html = driver.page_source 

infile=open("stack.html",'w') 
infile.write(html) 
infile.close() 

您必须安装硒包,以及Phantom.JS。 我测试过这段代码,它工作正常。让我知道你是否需要任何进一步的帮助!