English 中文(简体)
• 如何能自动增加谷歌警报器。
原标题:How Can I Automatically Add Google Alerts Using Python Mechanize

我知道这里销售的Adhury(http://oktaykilic.com/my-projects/google-alerts-api-python/),但我很想知道,为什么我现在做这项工作的我是徒劳的。

这就是我迄今为止所做的事情:

class GAlerts():

def __init__(self, uName =  USERNAME , passWord =  PASSWORD ):

    self.uName = uName
    self.passWord = passWord

def addAlert(self):

    self.cj = mechanize.CookieJar()
    loginURL =  https://www.google.com/accounts/ServiceLogin?hl=en&service=alerts&continue=http://www.google.com/alerts 
    alertsURL =  http://www.google.com/alerts 

    #log into google
    initialRequest = mechanize.Request(loginURL)
    response = mechanize.urlopen(initialRequest)

    #put in form info
    forms = ClientForm.ParseResponse(response, backwards_compat=False)
    forms[0][ Email ] = self.uName
    forms[0][ Passwd ] = self.passWord

    #click form and get cookies
    request2 = forms[0].click()
    response2 = mechanize.urlopen(request2)
    self.cj.extract_cookies(response, initialRequest)


    #now go to alerts page with cookies
    request3 = mechanize.Request(alertsURL)
    self.cj.add_cookie_header(request3)
    response3 = mechanize.urlopen(request3)

    #parse forms on this page
    formsAdd = ClientForm.ParseResponse(response3, backwards_compat=False)
    formsAdd[0][ q ] =  Hines Ward 

    #click it and submit
    request4 = formsAdd[0].click()
    self.cj.add_cookie_header(request4)
    response4 = mechanize.urlopen(request4)
    print response4.read()


myAlerter = GAlerts()
myAlerter.addAlert()

就我所知,它成功地在主页上登录并接上了警示,但在我进入问询和“立点”时,它把我送到一个页上,说“请进入有效的电子邮件地址”。 缺乏某种认证的Im? 我也不理解如何改变从神论中 custom倒的观念? 任何想法?

增 编

最佳回答

风俗习惯是用Javad 做的,因此,适当的解决办法是 figure URL 参数,然后试图照搬这些参数(这可能是它现在按预期操作的原因——你在用浏览器访问该遗址时通常会断出的URL 参数。

The lazy Solutions is to use the ><>>>galerts 图书馆确实看着你们需要什么。

涉及<条码>机械化的未来项目的几个方面 (或) 普通筛查:

  • Use Fiddler, an extremely useful HTTP debugging tool. It captures HTTP traffic from most browsers and allows you to see what exactly your browser requests. You can then craft the desired request manually and in case it doesn t work, you just have to compare. Tools like Firebug or Google Chrome s developer tools come in handy too, especially for lots of async requests. (you have to call set_proxies on your browser object to use it with Fiddler, see documentation)
  • For debugging purposes, do something like for f in self.forms(): print f. This shows you all forms mechanize recognized on a page, along with their name.
  • Handling cookies is repetitive, so - surprise! - there s an easy way to automate it. Just do this in your browser class constructor: self.set_cookiejar(cookielib.CookieJar()). This keeps track of cookies automatically.
  • I have been relying a long time on custom parses like BeautifulSoup (and I still use it for some special cases), but in most cases the fastest approach on web screen scraping is using XPath (for example, lxml has a very good implementation).
问题回答

Mechanize doesn t handle JavaScript, and those drop-down Menus are JS. If you want to do automatization where JavaScript is involved, I suggest using Selenium, which also has Python bindings.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签