suchen

Heim  >  Fragen und Antworten  >  Hauptteil

javascript - 为什么有些网站能显示内容但python.requests却爬不出数据呢?

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

代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<code>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()

</code>

大家讲道理大家讲道理2897 Tage vor963

Antworte allen(1)Ich werde antworten

  • ringa_lee

    ringa_lee2017-04-10 18:01:55

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    <code class="python">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()</code>

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

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

    Antwort
    0
  • StornierenAntwort