所以这是我的代码:
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'})
为什么会这样?我该如何修复?
您必须在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)