【问题标题】:Getting a Traceback error for a simple python script to scrape GoodReads.com urls获取用于抓取 GoodReads.com url 的简单 python 脚本的 Traceback 错误
【发布时间】:2019-11-24 05:50:41
【问题描述】:

这是一个脚本,用于获取书名列表 (BookTitles.txt),在网站 Goodreads 中搜索每个书名的第一个结果,并将 URL 列表返回到 csv 文件 (GoodReadsBooksNew.csv)。

我收到以下错误:

iii@iii:~$ 蟒蛇 /home/iii/AudioBookReviews/WebScraping/GoodreadsScraper.py

Traceback(最近一次调用最后一次):

文件“/home/iii/AudioBookReviews/WebScraping/GoodreadsScraper.py”, 第 72 行,在 create_csv_file() 中

文件“/home/iii/AudioBookReviews/WebScraping/GoodreadsScraper.py”, 第 29 行,在 create_csv_file 中 with open('/home/iii/AudioBookReviews/WebScraping/GoodReadsBooksNew.csv', 'w+', encoding='utf-8') as csv_file:

TypeError: 'encoding' 是该函数的无效关键字参数


from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException

from selenium.webdriver.firefox.options import Options
from selenium.webdriver.chrome.options import Options

from pyvirtualdisplay import Display
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common import keys
import csv
import time
import json

class Book:
    def __init__(self, title, url):
        self.title = title
        self.url = url
    def __iter__(self):
        return iter([self.title, self.url])

url = 'https://www.goodreads.com/'

def create_csv_file():
    header = ['Title', 'URL']
    with open('/home/iii/AudioBookReviews/WebScraping/GoodReadsBooksNew.csv', 'w+', encoding='utf-8') as csv_file:
        wr = csv.writer(csv_file, delimiter=',')
        wr.writerow(header)

def read_from_txt_file():
    lines = [line.rstrip('\n') for line in open('/home/iii/AudioBookReviews/WebScraping/BookTitles.txt', encoding='utf-8')]
    return lines

def init_selenium():
    chrome_options = Options()
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage') 
    options = Options()
    options.add_argument('--headless')
    global driver
    driver = webdriver.Chrome("/home/iii/AudioBookReviews/WebScraping/chromedriver",  chrome_options=chrome_options)
    driver.get(url)
    time.sleep(5)
    driver.get('https://www.goodreads.com/search?q=')

def search_for_title(title):
    driver.get('https://www.goodreads.com/search?q=')
    search_field = driver.find_element_by_name('q')
    search_field.clear()
    search_field.send_keys(title)
    search_field.send_keys(keys.Keys.RETURN) # you missed this part
    url = driver.find_element_by_xpath(
        '/html/body/div[2]/div[3]/div[1]/div[2]/div[2]/table/tbody/tr[1]/td[2]/a')
    print(url.get_attribute('href'))

def scrape_url():
    try:
        url = driver.find_element_by_css_selector('a.bookTitle').get_attribute('href')
    except:
        url = "N/A"

    return url

def write_into_csv_file(vendor):
   with open('/home/iii/AudioBookReviews/WebScraping/GoodReadsBooksNew.csv', 'a', encoding='utf-8') as csv_file:
        wr = csv.writer(csv_file, delimiter=',')
        wr.writerow(list(vendor))

create_csv_file()
titles = read_from_txt_file()    
init_selenium()

for title in titles:
    search_for_title(title)
    url = scrape_url()
    book = Book(title, url)
    write_into_csv_file(book)

【问题讨论】:

    标签: python web web-scraping


    【解决方案1】:

    我认为您使用的是python 2.7 版本。

    python 2.7 中的open 函数具有以下签名

    open(name[, mode[, buffering]])

    另一方面,python 3+ 具有以下签名

    open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
    

    【讨论】:

    • 这是否意味着只运行 python2... 应该修复它?
    • 如果您不想修改代码,请使用python 3+。或修改您的代码以适应您拥有的python 2+ 环境。
    • 你正在使用的打开函数是为python 3+而你所拥有的是python 2+
    猜你喜欢
    • 1970-01-01
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    • 2011-12-24
    • 2011-08-19
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多