search

Home  >  Q&A  >  body text

python打印中文乱码?

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

PHPzPHPz2889 days ago366

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

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    <code># -*- 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)</code>

    reply
    0
  • 天蓬老师

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

    Thank you very much! ! !

    reply
    0
  • Cancelreply