回复: python32 to write file

水静流深 1248283536 at qq.com
Sun Oct 23 09:59:19 EDT 2011


i change my code into :
import urllib.request, urllib.parse, urllib.error
import lxml.html
down='http://frux.wikispaces.com/'
root=urllib.request.urlopen(down).read()
root=lxml.html.fromstring(root)
file=root.xpath('//a')
for i  in  file:
    str1=i.text_content()
    if str1.find('pdf')  >-1 :
        str2='http://frux.wikispaces.com/file/view/'+str1
        myfile=urllib.request.urlopen(str2).read()
        book=open('c:/'+str1,'w')
        book.write(myfile)
        book.close()

the new problem is :

C:\Python32>python  c:\xml.py
Traceback (most recent call last):
  File "c:\xml.py", line 5, in <module>
    root=lxml.html.fromstring(root)
  File "C:\Python32\lib\site-packages\lxml\html\__init__.py", line 630, in froms
tring
    if start.startswith('<html') or start.startswith('<!doctype'):
TypeError: startswith first arg must be bytes or a tuple of bytes, not str 
------------------ 原始邮件 ------------------
发件人: "Chris Rebert"<clp2 at rebertia.com>;
发送时间: 2011年10月23日(星期天) 晚上9:48
收件人: "水静流深"<1248283536 at qq.com>; 
抄送: "python-list"<python-list at python.org>; 
主题: Re: python32 to write file

 
 2011/10/23 水静流深 <1248283536 at qq.com>:
<snip>
>         book=open('c:\'+str1,'w')  #  i  change it
<snip>> when i run it  in  python32,the output is  :
> book=open('c:\'+str1,'w')
> invalid  syntax,what is wrong?

Your problem is not at all Python 3-specific.
Backslashes are used for escape sequences in string literals (e.g.
"\n" is newline, "\t" is tab). For example, the string "c:\new\tally"
contains both a newline and a tab, but not an N, nor a T, nor any
backslashes (a literal backslash is written using the escape sequence
"\\"; i.e. two backslashes).
Similarly, "\'" is an escape sequence for apostrophe, and thus does
not terminate the string literal, leading to a not entirely obvious
SyntaxError.
Use forward slashes (/) instead; Windows accepts them instead of
backslashes as directory separators in path strings, and they have no
such escaping issues.

Cheers,
Chris
--
Damn you, CP/M!
http://rebertia.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20111023/cc8d9680/attachment.html>


More information about the Python-list mailing list