简体   繁体   English

机械化和斜纹说话

[英]Get mechanize and twill talking

I am writing a script for my employer to get certain data from their own site. 我正在为雇主编写脚本,以从他们自己的站点获取某些数据。 For a long list of reasons, I need to get the data off the site as it is shown. 由于一长串原因,我需要从显示的位置获取数据。 I have discovered, some of that data is retrieved via a js call... 我发现,其中一些数据是通过js调用检索的...

In retrospect, I should have gone with mechanize, but I went with twill (thinking I would have access to mechanize since twill describes itself as a thin-wrapper for mechanize) the project is just one post-call away from completion and I desperately do not want to rewrite all my twill based code for mechanize. 回想起来,我本来应该机械化的,但是我却选择了斜纹布(认为我可以使用机械化,因为斜纹布将自己描述为机械化的薄包装),该项目距离完成仅一个电话会议,我拼命地做不想重写我所有基于斜纹的代码以实现机械化。

I need to submit a post request that is not attached to a form. 我需要提交未附加在表格中的发帖请求。 So I need mechanize (or a similar lib). 所以我需要机械化(或类似的库)。 According to twill's docs, there should be a mechanize browser object retrievable by the below code: 根据twill的文档,应该有一个可通过以下代码检索的机械化浏览器对象:

#after logging in and successfully loading pages
b = get_browser()
mb = b._browser

I get: AttributeError: 'TwillBrowser' object has no attribute '_browser' 我得到: AttributeError: 'TwillBrowser' object has no attribute '_browser'

Looking at the source it seems twill's browser is not using mechanize's browser anymore? 从源头上看,斜纹布的浏览器似乎不再使用机械化的浏览器了吗?

So my questions are: 所以我的问题是:

  1. Is there still a mechanize browser inside twill. 斜纹布内是否仍然有一个机械化的浏览器。
  2. If so, how do I get it? 如果是这样,我如何得到它?
  3. If not, how can I get the cookies from twill to mechanize so I can make request using those cookies? 如果没有,我如何从斜纹中获取cookie进行机械化,以便可以使用这些cookie提出请求?

ideally I'd so something like: 理想情况下,我会这样:

cjar = 'cookies.yaml'
save_cookies(cjar)
mb.open(url, data=data, cookies=cjar)

But I know I need a little more magic than just adding cookies=filename :) 但我知道我需要的不仅仅是添加cookies = filename :)

  1. Is there still a mechanize browser inside twill. 斜纹布内是否仍然有一个机械化的浏览器。
  2. If so, how do I get it? 如果是这样,我如何得到它?

No. It is no longer built into twill. 不。它不再内置于斜纹里。 While pip installing twill still includes mechanize into twill's files, twill's code never imports those modules. 虽然pip安装斜纹仍然包括将机械化到斜纹的文件中,但是斜纹的代码从不导入那些模块。

  1. If not, how can I get the cookies from twill to mechanize so I can make request using those cookies? 如果没有,我如何从斜纹中获取cookie进行机械化,以便可以使用这些cookie提出请求?

From what I've read, there does not appear to be a simple or trivial way to do this. 从我阅读的内容来看,似乎没有一种简单或琐碎的方法可以做到这一点。 You can save_cookies from twill (but you can't customize how they are saved.). 您可以通过斜纹保存save_cookies(但无法自定义它们的保存方式。)。 Mechanize will need help to read the cookie files, and when it loads them it will need to ignore persistance, etc. Mechanize does give you the ability to do this, but I haven't found any usable code for this. Mechanize将需要帮助来读取cookie文件,并且在加载它们时将需要忽略持久性等。Mechanize确实使您能够执行此操作,但是我还没有找到任何可用的代码。
In my case, it was easier to write a method that would log me in in both twill and mechanize. 就我而言,编写一种可以同时以斜纹和机械化方式登录我的方法比较容易。

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

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