search

Home  >  Q&A  >  body text

网页爬虫 - python3爬链家utf8页面,中文全部是“南京小区二手房(南京链家网)”

迷茫迷茫2803 days ago1830

reply all(2)I'll reply

  • 阿神

    阿神2017-04-18 10:00:42

    # -*- coding: utf-8 -*-
    
    
    import requests
    from bs4 import BeautifulSoup
    
    url = 'http://nj.lianjia.com/xiaoqu/'
    html = requests.get(url)
    soup = BeautifulSoup(html.text.encode(html.encoding), 'lxml', from_encoding='utf-8')
    title = soup.title.get_text()
    print(title)
    

    1) Add # -- coding: utf-8 --statement
    2) Correctly handle the encoding of the returned response

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 10:00:42

    That’s right upstairs, in fact you can also use Latin1encoding to decode this text.

    import requests
    from bs4 import BeautifulSoup
    
    url = 'http://nj.lianjia.com/xiaoqu/'
    html = requests.get(url)
    soup = BeautifulSoup(html.text, 'lxml')
    title = soup.title.get_text()
    print(title.encode('latin1').decode('utf-8'))

    reply
    0
  • Cancelreply