I am trying to log to certain server and download pictures from there programatically. I know login and I was able to get which variables are username and password. I was also able to download pictures which needed no password with this code.
Web page keeps telling me my user agent is unsupported, so I assume I am setting user agent incorrectly.
It needs cookies and user-agent. I know how to login to pages with cookies but I do not know how to add User-agent parameter to HTTP request in Python (and check if it was correctly added).
I am building standard url opener. I am using this code :
def login(_url, _name, _password):
if 1:
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
opener.addheaders = {("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.开发者_开发问答0")}
urllib2.install_opener(opener)
#
# login parameters
# PRE:
# 1] addons.mozilla.org/en-US/firefox/addon/live-http-headers/
# 2] _url is first line, sometimes it ends with do-submit, php...
# 3] _name and _password is content
# SOURCE:
# http://www.oooff.com/php-scripts/basic-curl-form-filling-tutorial/php-newbie-form-fill-tutorial.php
#
else:
#
# i tried even mechanize
# but I do not know how to set it either
#
cookies = mechanize.CookieJar()
opener = mechanize.build_opener(mechanize.HTTPCookieProcessor(cookies))
params = urllib.urlencode(dict(login=_name, password=_password))
f = opener.open(_url, params)
data = f.read()
f.close()
return opener
I tried to move adheaders to differend places, but it did not help. Maybe it can be done with mechanize but I am not sure how.
Thanks for help. :)
The python docs page has this format for building openers using square brackets. You are passing it as {(...)}
import urllib2
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
opener.open('http://www.example.com/')
精彩评论