鱼C论坛

 找回密码
 立即注册
查看: 2617|回复: 4

[已解决]使用for对多个url进行爬取时,如何跳过其中无法访问的url

[复制链接]
发表于 2020-12-24 15:17:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 三一王 于 2020-12-24 16:02 编辑

如题。

源码如下:

  1. import urllib.request


  2. def url_open(url):
  3.     req = urllib.request.Request(url)
  4.     req.add_header('user-agent',
  5.                    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36')
  6.     response = urllib.request.urlopen(url)
  7.     html = response.read()
  8.     return html


  9. def find_m3u8url(url):
  10.     html = url_open(url).decode('utf-8')
  11.     m3u8url_dict = {}

  12.     url_a = html.find('file: "http:', 0) + 12


  13.     while url_a != -1:

  14.         url_b = html.find('.m3u8', url_a, url_a + 300)
  15.         if url_b != -1:

  16.             # 将带有中文字符的url地址转换为url编码
  17.             m3u8url = 'http:' + urllib.request.quote(html[url_a: url_b]) + '.m3u8'
  18.             print(m3u8url)

  19.             m3u8url_dict[html[url_a: url_b]] = m3u8url


  20.         break

  21.     return m3u8url_dict


  22. def save(m3u8url_dict):

  23.     for each in m3u8url_dict:

  24.         word = m3u8url_dict[each]

  25.         with open('m3u8urls.txt','a') as f:
  26.             f.write(each)
  27.             f.write('\n')
  28.             f.write(word)
  29.             f.write('\n')
  30.             f.write('\n')



  31. with open('edu_urls.txt','rb') as f:

  32.     for url in f:
  33.         url_open(url)

  34.         m3u8url_dict = find_m3u8url(url)
  35.         save(m3u8url_dict)



复制代码



在代码最后执行的这一步:
  1. with open('edu_urls.txt','rb') as f:

  2.     for url in f:
  3.         url_open(url)
  4.         ai_dict = find_m3u8url(url)
  5.         save(m3u8url_dict)
复制代码

因为文件“edu_urls.txt”中存在无法访问的url。
在执行时,如何跳过因无法访问而报错的url,继续对下个url进行操作?


恳求ing~~~~~
最佳答案
2020-12-24 15:45:37
url_open(url):
使用 try
...
except...
失败返回False
或者直接 pass
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-24 15:45:37 | 显示全部楼层    本楼为最佳答案   
url_open(url):
使用 try
...
except...
失败返回False
或者直接 pass
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2020-12-24 16:00:25 | 显示全部楼层
Cool_Breeze 发表于 2020-12-24 15:45
url_open(url):
使用 try
...
  1. with open('edu_urls.txt','rb') as f:

  2.     for url in f:
  3.         try:
  4.             url_open(url)

  5.             m3u8url_dict = find_m3u8url(url)
  6.             save(m3u8url_dict)
  7.         except:
  8.             pass
复制代码


是这样么,但是没有结果诶
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-12-24 16:16:51 | 显示全部楼层
找到问题了 ,多谢~~~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-24 16:18:13 | 显示全部楼层
三一王 发表于 2020-12-24 16:00
是这样么,但是没有结果诶
  1. def url_open(url):
  2.     req = urllib.request.Request(url)
  3.     req.add_header('user-agent',
  4.                    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36')
  5.     try:
  6.         response = urllib.request.urlopen(url)
  7.     except Exception:
  8.         print(f'{url} Fail')
  9.         return None
  10.     html = response.read()
  11.     return html
复制代码

  1. def find_m3u8url(url):
  2.     html = url_open(url).decode('utf-8')
  3.     if html is None: return None
  4.     m3u8url_dict = {}

  5.     url_a = html.find('file: "http:', 0) + 12


  6.     while url_a != -1:

  7.         url_b = html.find('.m3u8', url_a, url_a + 300)
  8.         if url_b != -1:

  9.             # 将带有中文字符的url地址转换为url编码
  10.             m3u8url = 'http:' + urllib.request.quote(html[url_a: url_b]) + '.m3u8'
  11.             print(m3u8url)

  12.             m3u8url_dict[html[url_a: url_b]] = m3u8url


  13.         break

  14.     return m3u8url_dict
复制代码

  1. with open('edu_urls.txt','rb') as f:

  2.     for url in f:
  3.         url_open(url)
  4.         ai_dict = find_m3u8url(url)
  5.         if ai_dict is None: contiune
  6.         save(m3u8url_dict)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-29 18:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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