我试图抓住一堆关于我所在地区的人口普查数据,从一个链接开始,以获得正确的代码。我想,最初,获取页面的标题,并将页面中的数据存储在以标题名称命名的txt文件中。
因此,例如在这种情况下,Census Block 970900-1-001 in Cortland County, New York
将是txt文件的标题。但是,当我尝试使用% variable
方法执行此操作时,它会向我显示错误Unsupported operand type for %: 'file' and 'unicode'
。我理解错误信息 - 我的问题是,我怎样才能实现我想要的功能,或者甚至可能实现?
到目前为止编写的代码:
from bs4 import BeautifulSoup
from urllib2 import urlopen
links = ['http://www.usa.com/NY0239709001001.html']
def block():
link = links[0]
html = urlopen(link)
soup = BeautifulSoup(html.read(),'lxml')
h1 = soup.find('h1').text
print(h1)
f = open('%s.txt','w') % h1
f.write(h1)
预期输出
创建名为Census Block 970900-1-001 in Cortland County, New York.txt
实际输出:
Unsupported operand type for %: 'file' and 'unicode'
答案 0 :(得分:4)
我猜你想要:
open('%s.txt' % h1, 'w')
即。您希望使用字符串格式生成要传递给open
的文件名 - 您不想打开名为'%s.txt'
的文件。
答案 1 :(得分:2)
将v.end() - 1
放在% h1
:
open()