Любые хитрости, чтобы исправить проблемы кодирования без метрической тонны .replace ()?Python3 Chrome-Driver BS4 - PullRequest
0 голосов
/ 28 декабря 2018

Команда print() идеально печатает удаленный веб-сайт в оболочку IDLE.Однако write / writelines / print не запишет в файл без большого количества ошибок кодирования или кода супер-гика-отряда.

Пробовал различные формы .encode(encoding='...',errors='...') безрезультатно.Когда я пробовал много разных кодировок, они превращались в форматы супер-гиков-отрядов или несколько? Внутри текстового файла.

Если бы я хотел потратить 10 лет на выполнение .replace('...','...'), как показано в кодеtext = ... Я могу заставить это работать полностью.

#! python3
import os
import os.path
from os import path
import requests
import bs4 as BeautifulSoup
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.options import Options


def Close():
    driver.stop_client()
    driver.close()
    driver.quit()

CHROMEDRIVER_PATH = 'E:\Downloads\chromedriver_win32\chromedriver.exe'

# start raw html
NovelName = 'Novel/Isekai-Maou-to-Shoukan-Shoujo-Dorei-Majutsu'
BaseURL = 'https://novelplanet.com'
url = '%(U)s/%(N)s' % {'U': BaseURL, "N": NovelName}

options = Options()
options.add_experimental_option("excludeSwitches",["ignore-certificate-errors"])
#options.add_argument("--headless") # Runs Chrome in headless mode.
#options.add_argument('--no-sandbox') # Bypass OS security model
#options.add_argument('--disable-gpu')  # applicable to windows os only
options.add_argument('start-maximized') # 
options.add_argument('disable-infobars')
#options.add_argument("--disable-extensions")
driver = webdriver.Chrome(CHROMEDRIVER_PATH, options=options)
driver.get(url)

# wait for title not be equal to "Please wait 5 seconds..."
wait = WebDriverWait(driver, 10)
wait.until(lambda driver: driver.title != "Please wait 5 seconds...")

soup = BeautifulSoup.BeautifulSoup(driver.page_source, 'html.parser')
# End raw html

# Start get first chapter html coded
i=0
for chapterLink in soup.find_all(class_='rowChapter'):
    i+=1
cLink = chapterLink.find('a').contents[0].strip()
print(driver.title)
# end get first chapter html coded

# start navigate to first chapter
link = driver.find_element_by_link_text(cLink)
link.click()
# end navigate to first chapter

# start copy of chapter and add to a file
def CopyChapter():
    wait = WebDriverWait(driver, 10)
    wait.until(lambda driver: driver.title != "Please wait 5 seconds...")
    print(driver.title)
    soup = BeautifulSoup.BeautifulSoup(driver.page_source, 'html.parser')
    readables = soup.find(id='divReadContent')
    text = readables.text.strip().replace('混','').replace('魔','').replace('族','').replace('デ','').replace('イ','').replace('ー','').replace('マ','').replace('ン','').replace('☆','').replace('ッ','Uh').replace('『','[').replace('』',']').replace('“','"').replace('”','"').replace('…','...').replace('ー','-').replace('○','0').replace('×','x').replace('《',' <<').replace('》','>> ').replace('「','"').replace('」','"')
    name = driver.title
    file_name = (name.replace('Read ',"").replace(' - NovelPlanet',"")+'.txt')
    print(file_name)
    #print(text) # <-- This shows the correct text in the shell with no errors
    with open(file_name,'a+') as file:
        print(text,file=file) # <- this never works without a bunch of .replace() where text is defined
    global lastURL
    lastURL = driver.current_url
    NextChapter()
# end copy of chapter and add to a file

# start goto next chapter if exists then return to copy chapter else Close()
def NextChapter():
    soup = BeautifulSoup.BeautifulSoup(driver.page_source, 'html.parser')
    a=0
    main = soup.find(class_='wrapper')
    for container in main.find_all(class_='container'):
        a+=1
    row = container.find(class_='row')
    b=0
    for chapterLink in row.find_all(class_='4u 12u(small)'):
        b+=1
    cLink = chapterLink.find('a').contents[0].strip()
    link = driver.find_element_by_link_text(cLink)
    link.click()
    wait = WebDriverWait(driver, 10)
    wait.until(lambda driver: driver.title != "Please wait 5 seconds...")
    global currentURL
    currentURL = driver.current_url
    if currentURL != lastURL:
        CopyChapter()
    else:
        print('Finished!!!')
        Close()
# end goto next chapter if exists then return to copy chapter else Close()

CopyChapter()
#EOF

Ожидаемые результаты будут иметь вывод текстового файла точно такой же, как у IDLE print(text), без каких-либо изменений.Тогда я смогу проверить, копируется ли каждая глава для просмотра в автономном режиме и останавливается ли она на последней опубликованной главе.

В настоящее время, если я не буду добавлять больше и больше .replace() для каждого романа и главы.это никогда не будет работать должным образом.Я не возражаю против удаления описаний объявлений вручную с помощью .replace(), но если есть и лучший способ сделать это, то, пожалуйста, научите меня.

Windows 10 Python 3.7.0

Тамбыла какая-то причина для os и os.path в более ранней версии этого скрипта, но теперь я не помню, нужна ли она по-прежнему или нет.

...