The reason that “the dog” returns a 400 Error is because you aren’t escaping the string for a URL.
If you do this:
import urllib, urllib2 quoted_query = urllib.quote(query) host = 'http://www.bing.com/search?q=%s&go=&qs=n&sk=&sc=8-13&first=%s' % (quoted_query, page) req = urllib2.Request(host) req.add_header('User-Agent', User_Agent) response = urllib2.urlopen(req)
It will work.
However I highly suggest you use requests instead of using urllib/urllib2/httplib. It’s much much easier and it’ll handle all of this for you.
This is the same code with python requests:
import requests results = requests.get("http://www.bing.com/search", params={'q': query, 'first': page}, headers={'User-Agent': user_agent})
Related Posts:
- Import error: No module name urllib2
- AttributeError(“‘str’ object has no attribute ‘read'”)
- AttributeError(“‘str’ object has no attribute ‘read'”)
- Need to install urllib2 for Python 3.5.1
- Making a POST call instead of GET using urllib2
- What is a “method” in Python?
- How to generate all permutations of a list?
- Is there a Python equivalent to Ruby’s string interpolation?
- Python Progress Bar
- Most efficient way to find mode in numpy array
- Python error “ImportError: No module named”
- How to groupby based on two columns in pandas?
- How do I append one string to another in Python?
- Python Progress Bar
- How to correct TypeError: Unicode-objects must be encoded before hashing?
- Python: list of lists
- How can I split and parse a string in Python?
- numpy matrix vector multiplication
- whitespace in regular expression
- How do I read CSV data into a record array in NumPy?
- Basic explanation of python functions
- TypeError: ‘Series’ objects are mutable, thus they cannot be hashed problemwith column
- How to rename a file using Python
- Cosine Similarity between 2 Number Lists
- How to install python3 version of package via pip on Ubuntu?
- django: TypeError: ‘tuple’ object is not callable
- ValueError: Length of values does not match length of index | Pandas DataFrame.unique()
- How to do superscripts and subscripts in Jupyter Notebook?
- How do I unload (reload) a Python module?
- ImportError: No module named PIL
- Maximum and Minimum values for ints
- How do I list all files of a directory?
- How to check whether a str(variable) is empty or not?
- Clear variable in python
- Python 3.6.0 syntax error “Missing parentheses in call to ‘print’ [duplicate]
- How to get coverage reporting when testing a pytest plugin?
- List of zeros in python
- How to remove an element from a list by index
- How to add title to subplots in Matplotlib
- ‘pip3’ is not recognized as an internal or external command, operable program or batch file
- Apply function to each element of a list
- How to display text in pygame?
- Python Nested Loops To Print Rectangle With Asterisks
- How to fix this strange error: “RuntimeError: CUDA error: out of memory”
- Generate random sentences in python
- Combine two columns of text in pandas dataframe
- How can I rename a conda environment?
- django import error – No module named core.management
- How to change a ‘LinearSegmentedColormap’ to a different distribution of color?
- Using python’s eval() vs. ast.literal_eval()
- ValueError: unsupported pickle protocol: 3, python2 pickle can not load the file dumped by python 3 pickle?
- Relative paths in Python
- Asyncio.gather vs asyncio.wait
- Loop backwards using indices in Python?
- Why do I get a “referenced before assignment” error when assigning to a global variable in a function?
- What does ‘index 0 is out of bounds for axis 0 with size 0’ mean?
- Generate random colors (RGB)
- Python import csv to list
- python SyntaxError: invalid syntax %matplotlib inline
- Count the number of occurrences of a character in a string
- Why does using from __future__ import print_function breaks Python2-style print?
- Download large file in python with requests
- Is there a “do … until” in Python?
- Printing variables in Python 3.4
- Meaning of @classmethod and @staticmethod for beginner?
- Haversine Formula in Python (Bearing and Distance between two GPS points)
- Replacing a character from a certain index
- ValueError: Expected object or value when reading json as pandas dataframe
- ImportError: No module named ‘google’
- ‘int’ object has no attribute ‘append’
- What does the “x for x in” syntax mean?
- Numpy extract submatrix
- How to check if a column exists in Pandas
- Python Add to dictionary loop
- how to exit a python script in an if statement
- How to add column to numpy array
- OpenCV NoneType object has no attribute shape
- AttributeError: ‘str’ object has no attribute ‘items’
- ImportError: No module named Crypto.Cipher
- Not able to pip install pickle in python 3.6
- How to print Unicode character in Python?
- Coalesce values from 2 columns into a single column in a pandas dataframe
- Unknown format code ‘f’ for object of type ‘str’- Folium
- Convert string to variable name in python
- Can not click on a Element: ElementClickInterceptedException in Splinter / Selenium
- How to split elements of a list?
- How can I find script’s directory?
- Porting Perl to Python
- Find object in list that has attribute equal to some value (that meets any condition)
- What is the purpose of the single underscore “_” variable in Python?
- Must have equal len keys and value when setting with an iterable
- Python IOError: File not open for reading
- Strange Exception in Tkinter callback
- How to resolve “ImportError: DLL load failed:” on Python?
- ‘module’ has no attribute ‘urlencode’
- Cannot concatenate ‘str’ and ‘float’ objects?
- Python Text Menu Infinite Loop
- Install pip for python 3.5
- How do I represent and work with n-bit vectors in Python?
- How to repeat individual characters in strings in Python