【发布时间】:2021-01-02 20:07:05
【问题描述】:
state_urls 是字典,其中 state 是所有美国州名作为键,url 是有关它们的信息的 url。以下代码的要点是:对于字典中的每个密钥对,以状态名称保存一个文本文件,并使其包含该状态密钥的链接的 html 内容。没有 '.content' 它可以正常工作,但是从页面返回的所有 html 比我需要的要多得多,但是一旦添加 .content 错误是 AttributeError: 'bytes' object has no attribute 'text'
for state, url in state_urls.items():
r = requests.get(url).content
with open(state_dir + state + '.txt', 'w') as file:
file.write(r.text)
sleep(2)
【问题讨论】:
-
使用 BeautifulSoup 解析获得有用的 html,我遇到了同样的问题,这对我有用
标签: python html web-scraping save disk