【发布时间】:2021-08-23 12:45:45
【问题描述】:
嗨,我有带有 youtube url 列表的 excel 文件,我试图获取它们的标题,因为它是 1000 个带有 3 个 excel 文件的完整列表,我尝试使用 python,但它太慢了,因为我不得不输入 sleep 命令html 渲染代码是这样的:
import xlrd
import time
from bs4 import BeautifulSoup
import requests
from xlutils.copy import copy
from requests_html import HTMLSession
loc = ("testt.xls")
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
wb2 = copy(wb)
sheet.cell_value(0, 0)
for i in range(3,sheet.nrows):
ytlink = (sheet.cell_value(i, 0))
session = HTMLSession()
response = session.get(ytlink)
response.html.render(sleep=3)
print(sheet.cell_value(i, 0))
print(ytlink)
element = BeautifulSoup(response.html.html, "lxml")
media = element.select_one('#container > h1').text
print(media)
s2 = wb2.get_sheet(0)
s2.write(i, 0, media)
wb2.save("testt.xls")
我的意思是无论如何让它更快我尝试了硒,但我猜它更慢。并且有了这个 html.render 我似乎需要使用“睡眠”计时器,否则它会给我错误我在睡眠时尝试了较低的值,但是在较低的睡眠值上一段时间后它会出错任何帮助,谢谢:)
ps:我放的打印只是为了检查输出,对使用并不重要。
【问题讨论】:
标签: python python-3.x beautifulsoup python-requests python-requests-html