request.exceptions.MissingSchema:无效的URL'channel_url':未提供任何模式。也许您是说http:// channel_url?

时间:2019-06-16 11:44:43

标签: python python-3.x python-requests

我想检查从CSV文件提供的Youtube频道的最新视频,并检索说明。

我收到此错误:

ERROR : requests.exceptions.MissingSchema: Invalid URL 'channel_url': No schema supplied. Perhaps you meant http://channel_url?

这是我的代码:

#!/usr/bin/env python

import requests
from bs4 import BeautifulSoup
import csv

with open('motovloggers.csv', 'r') as data_csv:
    data = csv.reader(data_csv)

    for channel in data:    
        url = channel[2]

        r = requests.get(url)

        soup = BeautifulSoup(r.content, 'html.parser')
        print(soup.select_one('.yt-lockup-title a')['title'])
        link = (soup.select_one('.yt-lockup-title a')['href'])

        # # new_link = link.replace('watch?v=', 'embed/')

        print('https://www.youtube.com' + link)

        # video = pafy.new(url)
        # print(video.description)

如何解决?

2 个答案:

答案 0 :(得分:0)

按照您所说的,您只需要跳过数据的第一个元素。因此,最简单的方法可能只是检查它是否包含字符串“ channel_url”

db.sequelize.knex.table('customers')
  .where('id', '=', ':customerId')
  .from(db.sequelize.knex.raw('('
    + 'SELECT'
    + 'COALESCE(SUM("month_value"), 0) AS month_value'
    + 'FROM "customer_products"'
    + 'WHERE "active" = true'
    + 'AND "customer_id" = :customerId'
    + ') src'
    + 'WHERE "id" = :customerId', [customerId]))
  .update({
    month_value: 'src.month_value',
    modified: 'NOW()',
});

或者您可以在该行中检查https:,如果没有出现,请继续。

答案 1 :(得分:0)

看起来channel_urlcsv文件中一列的标题。

因此,跳过文件的第一行可能会解决问题:

...
with open('motovloggers.csv', 'r') as data_csv:
    data = csv.reader(data_csv)

    next(data, None)  # <---- skip the first row

    for channel in data:
        ...