Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6736591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:08:52+00:00 2026-05-26T11:08:52+00:00

If I use Urllib2 to open a url using this: import urllib import urllib2

  • 0

If I use Urllib2 to open a url using this:

import urllib
import urllib2

url = 'http://www.bbc.co.uk'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
values = {}
headers = { 'User-Agent' : user_agent }

data = urllib.urlencode(values)
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
the_page = response.read()

It all works fine

But I want the mobile version so I set the user-agent to:

user_agent = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_2_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8G4 Safari/6533.18.5'

Which is what my iphone comes back with when go to a test page and read its headers

However if I run the above code with the user agent set to this urllib freaks out and seems to follow an indefinite 302 redirection loop which doesn’t occur when I visit the site on my iphone.

urllib2 comes back with a whole heap of debug info showing that it is following lots of 302’s and then finally:

urllib2.HTTPError: HTTP Error 301: The HTTP server returned a redirect error that would lead to an infinite loop.
The last 30x error message was:
Moved Permanently

Any ideas would be gratefully received.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:08:52+00:00Added an answer on May 26, 2026 at 11:08 am

    Your problem is redirect responses of your request.

    Try this lib to help you to handle redirect urls:

    http://pypi.python.org/pypi/requests/0.7.3
    or
    http://wwwsearch.sourceforge.net/mechanize/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using this code: def req(url, postfields): proxy_support = urllib2.ProxyHandler({http : 127.0.0.1:8118}) opener
I am trying to use urllib2 to open url and to send specific cookie
In Python I can use urllib2 (and urllib) to open external URLs such as
Is this Python script correct? import urllib, urllib2, cookielib username = 'myuser' password =
I'm trying to use URLLIB2 to open a URL and read back the contents
The site I'm trying to spider is using the javascript: request.open(POST, url, true); To
When I use urllib2 to make a HTTP 1.1 connection via a squid proxy,
I am using the following code to open a url and retrieve it's response
I am using the python urllib2 library for opening URL, and what I want
I need to get json data and I'm using urllib2: request = urllib2.Request(url) request.add_header('Accept-Encoding',

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.