我尝试将现有 URL 作为参数传递,以将其 HTML 加载到单个 txt
文件中:
for line in open('C:\Users\me\Desktop\URLS-HERE.txt'):
if line.startswith('http') and line.endswith('html\n') :
fichier = open("C:\Users\me\Desktop\other.txt", "a")
allhtml = urllib.urlopen(line)
fichier.write(allhtml)
fichier.close()
但我收到以下错误:
TypeError: expected a character buffer object
最佳答案
urllib.urlopen()
返回的值是一个类似文件的对象,一旦打开它,就应该使用 read()
方法读取它,如下所示如以下代码片段所示:
for line in open('C:\Users\me\Desktop\URLS-HERE.txt'):
if line.startswith('http') and line.endswith('html\n') :
fichier = open("C:\Users\me\Desktop\other.txt", "a")
allhtml = urllib.urlopen(line)
fichier.write(allhtml.read())
fichier.close()
希望这有帮助!
关于Python urlopen 返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12017358/