简体   繁体   中英

403 'Access Denied' Error when opening web page with urllib2 in Python

I'm trying to get definitions of words using Google and urllib2 by opening this url, https://www.google.com/search?q=define+<something> and parsing the source for the definition. However, when I try to access the page I get a 403 Error, supposedly to prevent data mining in this sort of fasion. I'm fairly sure it wouldn't be wise to try and bypass that, so i'm wondering if there's an alternative for accessing data from Google's servers, or a data dump I should be using.

Edit: Here is the extent of the code i'm using to access the URL;

url = "https://www.google.com/search?q=define+" + word
try:
    source = ulib.urlopen(url)
except ulib.HTTPError, e:
    print e.fp.read()

We would need to see your code for confirmation, but your question was probably answered here . In a nutshell, you need to define your user agent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM