简体   繁体   English

使用Python的Requests库导航网页/点击按钮

[英]Using Python's Requests library to navigate webpages / Click buttons

I'm new to web programming, and have recently began looking into using Python to automate some manual processes. 我是网络编程的新手,最近开始考虑使用Python来自动化一些手动流程。 What I'm trying to do is log into a site, click some drop-down menus to select settings, and run a report. 我要做的是登录网站,点击一些下拉菜单选择设置,然后运行报告。

I've found the acclaimed requests library: http://docs.python-requests.org/en/latest/user/advanced/#request-and-response-objects and have been trying to figure out how to use it. 我找到了备受赞誉的请求库: http//docs.python-requests.org/en/latest/user/advanced/#request-and-response-objects并且一直试图弄清楚如何使用它。

I've successfully logged in using bpbp's answer on this page: How to use Python to login to a webpage and retrieve cookies for later usage? 我已成功登录使用此页面上的bpbp答案: 如何使用Python登录网页并检索cookie以供日后使用?

My understanding of "clicking" a button is to write a post() command that mimics a click: Python - clicking a javascript button 我对“单击”按钮的理解是编写一个模仿单击的post()命令: Python - 单击一个javascript按钮

My question (since I'm new to web programming and this library) is how I would go about pulling the data I need to figure out how I would construct these commands. 我的问题(因为我是网络编程和这个库的新手)是我如何去绘制我需要弄清楚如何构建这些命令的数据。 I've been looking into [RequestObject].headers, .text, etc. Any examples would be great. 我一直在研究[RequestObject] .headers,.text等。任何例子都会很棒。

As always, thanks for your help! 一如既往,感谢您的帮助!

EDIT::: To make this question more concrete, I'm having trouble interacting with different aspects of a web-page. 编辑:::为了使这个问题更具体,我无法与网页的不同方面进行交互。 The following image shows what I'm actually trying to do: 下图显示了我实际上要做的事情:

例

I'm on a web-page that looks like this. 我在一个看起来像这样的网页上。 There is a drop-down menu with click-able dates that can be changed. 有一个下拉菜单,其中包含可以更改的可点击日期。 My goal is to automate changing the date to the most recent date, "click"'Save and Run', and download the report when it's finished running. 我的目标是自动将日期更改为最近的日期,“单击”“保存并运行”,并在完成运行时下载报告。

The only solution to this I have found is Selenium . 我发现的唯一解决方案是Selenium If it werent a javascript heavy website you could try mechanize but for this you need to render the javascript and then inject javascript...like Selenium does. 如果它不是一个javascript重的网站,你可以尝试机械化,但为此你需要渲染javascript,然后注入javascript ...像Selenium一样。

Upside: You can record actions in Firefox (using selenium) then export those actions to python. 好处:您可以在Firefox中记录操作(使用selenium),然后将这些操作导出到python。 The downside is that this code has to open a browser window to run. 缺点是此代码必须打开浏览器窗口才能运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM