TypeError: expected string or bytes-like..

 时间:2024-11-06 23:51:12

1、代码,如下所示:import refrom crawling import downloadimport urllib.parsedef link_crawler(seed_url,link_regex): """Crawl from the given seed URL following links matched b link_regex """ crawl_queue = [seed_url] while crawl_queue: url = crawl_queue.pop() html = download(url) #filter for links matching our regular expressiong for link in get_links(html): if re.match(link_regex,link): crawl_queue.append(link)def get_links(html): """Return a list of links from html""" # a regular expression to extract all links from the webpage webpage_regex = re.compile('<a[^>]+href=["\'](.*?)["\']', re.IGNORECASE) #List of all links from the webpage return webpage_regex.findall(html)link_crawler('http://example.webscraping.com','/(index|view)')

TypeError: expected string or bytes-like..

2、运行之后报错:TypeError: expected string or bytes-like object

3、分析可得是:字符串相关问题这里看一下findall:re.findall(pattern, string[, flags]):解释为:搜索string,以列表形式返回全部能匹配的子串。

TypeError: expected string or bytes-like..TypeError: expected string or bytes-like..

4、所以我们要将return webpage_regex.findall(html)中的html 改为str(html)

TypeError: expected string or bytes-like..

5、运行后,成功。如果您觉得有帮助的话,帮我投个票吧。

TypeError: expected string or bytes-like..
  • Windows如何使用FFMPEG给MP4文件切片并生成m3u8
  • 集装箱如何装箱
  • ORCAD中调用封装元件的方法
  • sqlitestudio怎么编写sql语句
  • 东北一锅出怎么做?
  • 热门搜索
    痛风怎么办快速止痛 美菱冰箱质量怎么样 为什么手机充电很慢 网络电视怎么看电视台节目 消化不良怎么办 脚趾缝烂脱皮痒怎么办 数据有效性怎么设置 拉屎为什么会出血 鸡蛋饼的做法大全 杨幂为什么叫大幂幂