【发布时间】:2016-01-05 13:12:47
【问题描述】:
我不明白为什么下面的代码总是产生一个空字符串。我正在尝试获取将网站内容提取到“txt”文件的代码,但它只是不断产生一个空字符串。代码有错误吗?
import urllib3
import certifi
# Function: Convert information within html document to a text file
# Append information to the file
def html_to_text(source_html, target_file):
http = urllib3.PoolManager(
cert_reqs='CERT_REQUIRED', # Force certificate check.
ca_certs=certifi.where(), # Path to the Certifi Bundle
headers={'connection': 'keep-alive', 'user-agent': 'Mozilla/5.0', 'accept-encoding': 'gzip, deflate'},
)
r = http.urlopen('GET', source_html)
print(source_html)
response = r.read().decode('utf-8')
# TODO: Find the problem that keeps making the code produce an empty string
print(response)
temp_file = open(target_file, 'w+')
temp_file.write(response)
source_address = "https://sg.finance.yahoo.com/lookup/all?s=*&t=A&m=SG&r=&b=0"
target_location = "C:\\Users\\Admin\\PycharmProjects\\TheLastPuff\\Source\\yahoo_ticker_symbols.txt"
html_to_text(source_address, target_location)
【问题讨论】:
-
当您说“生产”时,您的意思是“打印”、“写入文件”还是“打印和写入文件”?
print(source_html)和print(response)是否打印任何内容? -
打印和写入功能都没有产生任何东西。 “print(source_html)”确实成功打印了“source_address”。
-
r对象似乎有一个包含响应正文的r.data属性。 urllib3.readthedocs.org/en/latest/#usage -
@Cloud 我刚刚在我的电脑上测试了它,它工作得很好,它打印并在文件中写入了网站源代码。
-
难道不应该尊重站长不被刮的愿望吗?
标签: python python-3.x urllib3