Why two type result for my python script?
i have script like this
import mechanize url = "http://www.globalhide.com/browse.php?u=u=http://www.whoisxmlapi.com/whoisserver/WhoisService?domainName=google.com" br = mechanize.Browser() br.set_handle_robots(False) br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:188.8.131.52) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')] response = br.open(url) content = response.read() f = open('q.html', 'w') f.write(content) f.close()
i need the result like this when i run this from python shell ,the result correct. but when i save this in a something.py file and run like this python something.py the content of q.html What wrong with my code?
I don't think there's anything wrong with your code as such. Changing the requested url results in good data returned.
The block is being implemented by globalhide.com themselves. The link you added in your question gives the same page as you've attached (more or less). I can't tell you exactly how this hotlink blocking is implemented, but it could be through the referrer heading. Looking into referrer spoofing may help you here.
Jumped the gun a little bit there wrt referrer spoofing. I'd go with Aaron's cookie suggestion.
For that URL, I sometimes get the XML, and sometimes get the "no hotlinking" page in Chrome Linux. The first hit returns the no-hotlinking page for the same url. If I clear my cookies and visit that page again, I get the no-hotlinking image.
It appears that a Cookie is required for this site. The following should work with your code.
policy = mechanize.DefaultCookiePolicy(rfc2965=True) cj = mechanize.LWPCookieJar(policy=policy) br.set_cookiejar(cj)
For more information on the different methods to implement cookies, check out Mechanize Docs - Cookies.
Edit 1 You should save the cookie jar, see Cookielib - Save.
Edit 2 here's the cookie information that site set for me:
Name: __utma Content: 53296278.1653562620.1363413018.1311413018.1337443014.1 Domain: .globalhide.com Path: / Send for: Any kind of connection Accessible to script: Yes Created: Wednesday, May 1, 2013 6:56:58 AM Expires: Friday, May 1, 2015 6:56:58 AM Name: s Content: x2tjlhb1qfidn5t1ds8kvd24p5 Domain: www.globalhide.com Path: / Send for: Any kind of connection Accessible to script: Yes Created: Wednesday, May 1, 2013 6:56:57 AM Expires: When the browsing session ends