繁体   English   中英

想要从网站上获取信息,但使用Python显示“ 250禁止”

[英]Want to fetch information from a website but it shows “250 forbidden” with Python

我正在使用Python从网站获取信息。 该脚本非常简单:

from urllib2 import *

website='http://www.haodf.com'
web=urlopen(website)
content=web.read()#This makes python visit and fetch the content of the website

print content

并返回:

    <?xml version="1.0" encoding="utf-8"?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
        <html>
        <head>
        <title>250 Forbidden</title>
        </head>
        <body>
        <h1>250 Forbidden</h1>
        </body>
        </html>

为什么内容中有“ 250 Forbidden”? 看来我实际上无法访问该网站,尽管该脚本在与google.com等其他网站打交道时可以使用。

这个特定的网站要求User-Agent标头与请求一起发送:

>>> import urllib2
>>> request = urllib2.Request("http://www.haodf.com", headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36'})
>>> print urllib2.urlopen(request).read()
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
...

或切换到requests (默认情况下发送User-Agent ):

>>> import requests
>>> response = requests.get('http://www.haodf.com')
>>> response.request.headers
CaseInsensitiveDict({'Accept-Encoding': 'gzip, deflate, compress', 'Accept': '*/*', 'User-Agent': 'python-requests/2.2.1 CPython/2.7.5 Darwin/13.3.0'})

>>> print response.text
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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