【发布时间】:2020-04-18 14:02:35
【问题描述】:
(stackoverflow 上的第一篇文章)
我正在尝试使用urllib3 上的python 3.7.1 从该页面"https://nyaa.crystalyx.net/search?q=Kuzu+no+Honkai" 下载源代码。
我创建了以下函数来将源代码保存在文件中:
def get_source_code(url : str):
print(url,len(url))
os.system("pause")
http = urllib3.PoolManager()
r = http.request('GET', url)
content = str(r.data)
#print(content)
#Saves the source_code in a file
source_code = open("source_code.txt","w+")
for letter in content:
source_code.write(letter)
source_code.close()
#Saves the elements of the source code in a list of element splitted by "\n" then deletes the initial file
source_code = open("source_code.txt","r+")
content = (source_code.readline()).split("\\n")
source_code.close()
#os.system("pause")
os.remove("source_code.txt")
#Creates a new file containing the source_code correctly displayed
source_code = open("source_code.txt","w+")
for element in content:
source_code.write(element + '\n')
source_code.close()
当我这样调用我的函数时,一切正常:
get_source_code("https://nyaa.crystalyx.net/search?q=Kuzu+no+Honkai")
(你可以在这里查看输出https://pastebin.com/SBumCH3b)
所以我尝试使用input() 以更用户友好的方式调用我的函数
to_download = str(input("Enter the name of the anime you wanna download: "))
to_download = to_download.replace(" ","+")
to_download = str("https://nyaa.crystalyx.net/search?q=") + str(to_download)
get_source_code(to_download)
这最终在我的文件中给了我一个非常不同且不完整的源代码 (你可以在这里查看输出https://pastebin.com/bq0dqeZw)
我已经检查了给get_source_code() 的两个字符串是否相同且长度相同
如果有人可以帮助我,那就太好了。 谢谢。
【问题讨论】:
-
你输入了什么?
-
正如我所说,我第一次输入(当它工作时)
get_source_code("https://nyaa.crystalyx.net/search?q=Kuzu+no+Honkai")然后第二次(它没有工作)get_source_code(to_download) -
在调用函数之前,我有
print(to_download) = https://nyaa.crystalyx.net/search?q=Kuzu+no+Honkai所以我没有得到问题 -
在cmd中我写了
Kuzu no Honkai
标签: python html python-3.x urllib urllib3