【问题标题】:Can't get the source code from a page when not typing the name manually不手动输入名称时无法从页面获取源代码
【发布时间】: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


【解决方案1】:

在您的第二个查询中拼写错误,因此是错误的。在第一个屏幕截图中,您粘贴查询是

required type="search" value="Kuzu no Honkai">

在您接受输入的第二个查询中,它的拼写略有不同

required type="search" value="Kozu no Honkai">

注意它是怎么说的 Kozu 而不是 Kuzu。输入的时候好像拼错了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 2015-06-22
    • 1970-01-01
    • 2012-07-22
    • 2010-11-24
    • 1970-01-01
    相关资源
    最近更新 更多