为什么这会给我这个虫子?“发生异常:InvalidSchema”

2024-04-29 02:53:11 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的代码:

import requests

req = requests.post('<a href="https://en.wikipedia.org/w/index.php">https://en.wikipedia.org/w/index.php</a>', data = {'search':'Nanotechnology'})

req.raise_for_status()

with open('Nanotechnology.html', 'wb') as fd:

    for chunk in req.iter_content(chunk_size=50000):

        fd.write(chunk)

这给了我一个错误:

Exception has occurred: InvalidSchema
No connection adapters were found for '<a href="https://en.wikipedia.org/w/index.php">https://en.wikipedia.org/w/index.php</a>'
  File "/Users/lik20/Downloads/request/downloadingawebpage.gyp", line 3, in <module>
    req = requests.post('<a href="https://en.wikipedia.org/w/index.php">https://en.wikipedia.org/w/index.php</a>', data = {'search':'Nanotechnology'})

这是为什么?我如何修复它


1条回答
网友
1楼 · 发布于 2024-04-29 02:53:11

必须在req变量中输入站点的URL作为第一个参数,而不是HTML结构的一部分

import requests

req = requests.post('https://en.wikipedia.org/w/index.php', data = {'search':'Nanotechnology'})

req.raise_for_status()

with open('Nanotechnology.html', 'wb') as fd:

    for chunk in req.iter_content(chunk_size=50000):

        fd.write(chunk)

相关问题 更多 >