【问题标题】:Python Webscraping: Problems parsing chinese characters with beautiful soup/requestsPython Webscraping:用漂亮的汤/请求解析汉字的问题
【发布时间】:2021-03-28 23:22:45
【问题描述】:

我正在抓取一个中文网站,通常解析中文字符是没有问题的,我用 bs4 中的模式函数来查找特定的 url。 但是,对于这个特定的中文网站,无法正确解析汤。 下面是我用来设置汤的代码:

start = f'http://www.shuichan.cc/news_list.asp?action=&c_id=93&s_id=210&page={1}'
r = requests.get(start)
soup = bs(r.content, "html.parser")

打印汤的一个例子如下:

Current soup

注意:我必须将图片添加为堆栈,尽管它是垃圾邮件:)

以上内容应如下所示:

Proper soup

我想知道我是否必须在请求中指定某种编码,或者在汤中指定某种编码,但目前我还没有找到任何可行的方法。

提前致谢!

【问题讨论】:

    标签: python web-scraping beautifulsoup request


    【解决方案1】:

    我不懂中文。这会产生预期的结果吗?

    import requests
    from bs4 import BeautifulSoup as bs
    
    start = f'http://www.shuichan.cc/news_list.asp?action=&c_id=93&s_id=210&page={1}'
    r = requests.get(start)
    soup = bs(r.content.decode('GBK', 'ignore'), "html.parser")
    
    print(soup)
    

    【讨论】:

    • 是的 - 谢谢你的帮助!
    • 你能详细说明修复的作用吗?
    • 这只是网站的字符集。只需要对其进行编码。可以阅读更多here
    • 这正是我抓取中文网站所需要的。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2013-03-21
    • 2019-11-10
    • 2021-06-22
    • 1970-01-01
    • 2015-07-03
    • 1970-01-01
    • 2017-05-23
    • 2021-03-06
    相关资源
    最近更新 更多