使用硒并写入文件时出错

时间:2018-11-11 23:15:00

标签: python-3.x selenium beautifulsoup

import time

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
import re
import pandas as pd
from pytube import YouTube


browser = webdriver.Chrome("C:/Users/Downloads/chromedriver_win32/chromedriver.exe")



browser.get("https://www.youtube.com/channel/UCaKt8dvEIPnEHWSbLYhzrxg/videos")
time.sleep(1)

elem = browser.find_element_by_tag_name("body")

no_of_pagedowns = 100

while no_of_pagedowns:
    elem.send_keys(Keys.PAGE_DOWN)
    time.sleep(0.2)
    no_of_pagedowns-=1

html = browser.page_source
soup = BeautifulSoup(html, "lxml")
tags = soup.find_all('a')
fname = "C:/Stock_in_CFD/Output.txt"
text_file = open(fname, "w+", encoding="utf-8")
for tag in tags:
    t = tag.get('href')
    text_file.write(t)

当我运行上面的代码时。我遇到错误

TypeError:write()参数必须为str,而不是None

当我不使用硒时,我可以做到。 我正在使用硒,因为我想在解析之前先向下滚动整个页面,然后再使用BeautifulSoup

0 个答案:

没有答案