鱼C论坛

 找回密码
 立即注册
查看: 1973|回复: 2

新人求问:爬虫中的编码问题

[复制链接]
发表于 2017-9-29 20:45:00 | 显示全部楼层 |阅读模式
5鱼币
新人求问,本人要爬取b站首页的全部视频链接,但是爬取时报了编码错误:TypeError: can't use a string pattern on a bytes-like object。
改了好久还是不对。请教一下究竟该怎么改啊???急!!!

  1. # !/usr/bin/env python
  2. # -*- coding:utf-8 -*-

  3. import download_url
  4. import re
  5. from urllib import parse
  6. import chardet

  7. def link_crawler(seed_url, link_regex):
  8.     crawl_queue = [seed_url]
  9.     seen = set(crawl_queue)
  10.     while crawl_queue:
  11.         url = crawl_queue.pop()
  12.         html = download_url.download_url(url)
  13.         for link in get_links(html):
  14.             if re.match(link_regex, link):
  15.                 link = parse.urljoin(seed_url, link)
  16.                 if link not in seen:
  17.                     seen.add(link)
  18.                     crawl_queue.append(link)

  19. def get_links(html):
  20.     webpage_regex = re.compile(r"(?<=href=").+?(?=")|(?<=href=\').+?(?=\')", re.I|re.S|re.M)
  21.     charset = type(html)
  22.     if charset != 'str':
  23.         html = html.encode('utf-8')
  24.     return re.findall(webpage_regex, html)

  25. link_crawler('https://www.bilibili.com', '/video')
复制代码



想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-9-29 20:52:19 | 显示全部楼层
询问请把问题描述清楚,代码上全。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-9-29 20:59:09 | 显示全部楼层
ba21 发表于 2017-9-29 20:52
询问请把问题描述清楚,代码上全。

我的问题就是前面说的那样,代码运行时报错了,报错内容是:TypeError: can't use a string pattern on a bytes-like object
我想知道哪里错了,应该怎么改?这样说能够明白吗?真心求教。
前面导入了一个之前编写的下载网页的文件, download_url.py。下面附代码
  1. # !/usr/bin/env python
  2. # -*- coding:utf-8 -*-

  3. from urllib import request, error
  4. import chardet

  5. # 抓取网页内容
  6. def download_url(url, num_retries = 2):
  7.     print('正在下载网页:', url)
  8.     try:
  9.         # 设置用户代理
  10.         headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
  11.                                  'Chrome/52.0.2743.116 Safari/537.36 Edge/15.16193'}
  12.         req = request.Request(url, headers=headers)
  13.         html = request.urlopen(req).read()

  14.         # 匹配网页编码方式
  15.         charset = chardet.detect(html)['encoding']
  16.         if charset == 'utf-8':
  17.             html = html.decode('utf-8')
  18.         elif charset == 'gbk' or charset == 'gb2312' or charset == 'GB2312':
  19.             html = html.decode('GB18030')

  20.     except error.URLError as err:
  21.         print('Download error', err)
  22.         html = None

  23.         # 遇到5xx错误(服务端错误)时重新下载网页两次
  24.         if num_retries > 0:
  25.             if hasattr(err, 'code') and 500 <= err.code < 600:
  26.                 return download_url(url, num_retries-1)

  27.     return html

  28. if __name__ == '__main__':
  29.     download_url(url)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 04:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表