search

Home  >  Q&A  >  body text

python打印中文乱码?

打印出的中文内容全是乱码
但是print('中文')可以打印出中文

PHPzPHPz2890 days ago367

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 17:39:53

    See this previous post
    https://segmentfault.com/q/1010000004887283/a-1020000004887790

    Need to declare the encoding format of the file

    # -*- coding: utf-8 -*-
    
    from bs4 import BeautifulSoup
    import requests
    
    url = "http://news.baidu.com"
    data = requests.get(url)
    s = BeautifulSoup(data.text.encode(data.encoding), "lxml")
    title = s.select("#pane-news > p > ul > li.hdline0")
    print(title)

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 17:39:53

    Thank you very much! ! !

    reply
    0
  • Cancelreply