Ошибка: TypeError: string indices must be integers
В чем ошибка?
import requests
from bs4 import BeautifulSoup
import csv
FILE="games.csv"
def get_html(url):
r = requests.get(url)
r.encoding = 'utf8'
return r.text
def get_link(html):
soup = BeautifulSoup(html, 'lxml')
video = soup.find('div', attrs={"class": "highlight_player_item", "data-mp4-hd-source": True})
for item in video:
games.append({'video':item["data-mp4-hd-source"]})
def save_file(items, path):
with open(path, 'w', newline='', encoding='cp1251') as file:
writer = csv.writer(file, delimiter=';')
writer.writerow(['VIDEOS'])
for item in items:
writer.writerow([item['video']])
games=[]
with open("racesru.csv", newline='', encoding='cp1251') as csvfile:
reader = csv.reader(csvfile, delimiter=';')
for row in reader:
link = row[4]
if (link != '0' and link != 'LINK'):
url = link
print(url)
print(games)
save_file(games, FILE)
get_link(get_html(str(url)))
Ответы (1 шт):
Автор решения: vp_arth
→ Ссылка
Переменная item в вашем коде содержит строку. У строки нет символа с номером data-mp4-hd-source.
Сложно понять, что вы хотели выразить этим кодом, но предположу, что достаточно будет добавлять сами итемы:
games.append({'video': item})
