请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

老师这个问题研究一晚上了还是没搞定。

用html = str(html,encoding=‘utf-8’)时,报
’utf-8’ codec can’t decode byte 0xd0 in position 1764: invalid continuation byte
如果去掉encoding,运行不报错,但是打印不出内容。代码如下:

import re
from urllib import request

class Spider():
    url = 'http://data.eastmoney.com/bkzj/hy.html'
    root_pattern = '<tbody>[\\s\\S]*?</tbody>'

    def __fetch_content(self):
        r = request.urlopen(Spider.url)
        html = r.read()
        # print(type(html))
        html = str(html,encoding='utf-8')
        # print(html)
        return html

    def __analysis(self,html):
        root_html = re.findall(Spider.root_pattern,html)
        # print(type(root_html))
        print(root_html[1])

    def go(self):
        html = self.__fetch_content()
        self.__analysis(html)
        pass
spider = Spider()
spider.go()

正在回答 回答被采纳积分+3

1回答

7七月 2020-10-13 13:32:11

不同的网站会有很多不一样的地方,不是所有网站都通用的。这个要根据具体网站的特点做调整,需要研究

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信