相关疑难解决方法(0)

builtins.TypeError:必须是str,而不是字节

我已经将我的脚本从python 2.7转换为3.2,我有一些bug.

# -*- coding: utf-8 -*-
import time
from datetime import date
from lxml import etree
from collections import OrderedDict

# Create the root element
page = etree.Element('results')

# Make a new document tree
doc = etree.ElementTree(page)

# Add the subelements
pageElement = etree.SubElement(page, 'Country',Tim = 'Now', 
                                      name='Germany', AnotherParameter = 'Bye',
                                      Code='DE',
                                      Storage='Basic')
pageElement = etree.SubElement(page, 'City', 
                                      name='Germany',
                                      Code='PZ',
                                      Storage='Basic',AnotherParameter = 'Hello')
# For multiple multiple attributes, use as shown above

# Save to XML file
outFile = open('output.xml', …
Run Code Online (Sandbox Code Playgroud)

python lxml python-3.x

185
推荐指数
2
解决办法
18万
查看次数

使用python将某些网站的HTML保存在txt文件中

我需要在txt文件中保存任何网站的HTML代码,这是一个非常简单的练习,但我对此有疑问,因为有一个函数可以做到这一点:

import urllib.request

def get_html(url):
    f=open('htmlcode.txt','w')
    page=urllib.request.urlopen(url)
    pagetext=page.read() ## Save the html and later save in the file
    f.write(pagetext)
    f.close()
Run Code Online (Sandbox Code Playgroud)

但这不起作用.

html python parsing urllib python-3.x

9
推荐指数
2
解决办法
2万
查看次数

标签 统计

python ×2

python-3.x ×2

html ×1

lxml ×1

parsing ×1

urllib ×1