javascript - 为什么有些网站能显示内容但python.requests却爬不出数据呢?
大家讲道理
大家讲道理 2017-04-10 17:59:55
0
1
808

打算在http://app1.sfda.gov.cn/上获取一些数据整理起来,想用python.requests实现一个小爬虫来获取数据,但是python.requests会一直报('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer')的错误,但是那个网站是的的确确能上的,各位有知道我错在哪里吗,谢谢??

代码如下:

import requests
def testLoadRequest():
    officialHeader = {
        'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) '
                     'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36',
        'referer':'http://app1.sfda.gov.cn/',
        'Upgrade-Insecure-Requests':'1'
    }
    officialUrl = 'http://app1.sfda.gov.cn';
    try:
        officialRequest = requests.get(officialUrl, headers= officialHeader)
        print(officialRequest.content)
    except Exception as e:
        print(e)
testLoadRequest()
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

membalas semua(1)
左手右手慢动作
import requests
def testLoadRequest():
    officialHeader = {
        'User-Agent':'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)',
        'referer':'http://app1.sfda.gov.cn/datasearch/face3/dir.html',
        'Upgrade-Insecure-Requests':'1'
    }
    officialUrl = 'http://app1.sfda.gov.cn/datasearch/face3/dir.html';
    try:
        officialRequest = requests.get(officialUrl, headers= officialHeader)
        print(officialRequest.content)
    except Exception as e:
        print(e)
testLoadRequest()

以上代码成功
改下网址
你的user_agent写了两行? 另外我换了ie9的
print(r.content)中的r是什么,写成r还不报错,我也不懂是因为try?

我狂点运行,控制台让我与网站管理员联系,我现在网站打不开了,不懂。难道被我一秒三次的请求攻陷了?
话说这种网站根本不需要headers吧

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!