当前位置: 首页 > 知识库问答 >
问题:

我需要使用Python登录Goodreads

沈成天
2023-03-14

使用python代码从Goodreads获得书籍的描述,但是我的代码不允许我在开始实际搜索之前登录,我显然需要添加代码来使用我的用户名和pw进行登录,然后执行搜索。

def fetch_description_genre_and_date_from_goodreads(self, book_author, book_title):
    """Fetch book description from Goodreads."""
    self.driver.get('http://goodreads.com/search?query=' + urllib.parse.quote_plus('{} {}'.format(book_author, book_title)))
    genre = None
    publish_date = None
    try:
        self.driver.find_elements_by_class_name('bookTitle')[0].click()
        try:
            WebDriverWait(self.driver, 5).until(
                EC.presence_of_element_located((By.CLASS_NAME, 'bookPageGenreLink')))
            genre = self.driver.find_element_by_class_name('bookPageGenreLink').text
        except TimeoutException:
            pass
        try:
            WebDriverWait(self.driver, 5).until(
                EC.presence_of_element_located((By.XPATH, "//div[@id='details']/div[2]")))
            publish_date = self.driver.find_element_by_xpath("//div[@id='details']/div[2]").text.split('by')[0].rstrip()
            if not 'published' in publish_date.lower():
                publish_date = None
        except TimeoutException:
            pass
        long_description = False
        WebDriverWait(self.driver, 5).until(
            EC.presence_of_element_located((By.ID, 'description')))
        for anchor_tag in self.driver.find_element_by_id('description').find_elements_by_tag_name('a'):
            if anchor_tag.text == '...more':
                anchor_tag.click()
                long_description = True
                break
        return (self.driver.find_elements_by_xpath("//span[starts-with(@id,'freeText')]")[1].text, genre, publish_date)\
            if long_description else (self.driver.find_elements_by_xpath("//span[starts-with(@id,'freeText')]")[0].text, genre, publish_date)
    except (IndexError, TimeoutException):
        return None, genre, publish_date

现在发生的是,我首先得到了登录的弹出窗口,然后出现了错误:

Message: unknown error: Element <a class="bookTitle" itemprop="url" 
href="/book/show/41880080-all-that-you-leave-behind?from_search=true">...</a> is 
not clickable at point (214, 326). Other element would receive the click: <div class="loginModal__popUpBook" data-reactid=".2z3blpsmww.0.1.2"></div>

请问有人能帮我一下吗?

共有1个答案

万俟嘉珍
2023-03-14

我有个解决办法,如果这对其他人有帮助的话。我没有尝试在模式弹出窗口中单击,而是移动光标并单击页面本身,这将移除弹出窗口。然后代码正确地进行。我在:self.driver.execute_script('el=document.elementfrompoint(0,30);el.click();')之后添加了以下内容:self.driver.get('http://goodreads.com/search?query='+urllib.parse.quote_plus('{}{}'.format(book_author,book_title))

这是百分之百有效的。谢谢你想帮忙。

 类似资料:
  • 问题内容: 首先,我认为值得一提,我知道有很多类似的问题,但是没有一个对我有用。 我是Python,html和网络抓取工具的新手。我正在尝试从需要先登录的网站上抓取用户信息。在我的测试中,我以来自github的scraper我的电子邮件设置为例。主页是“ https://github.com/login ”,目标页面是“ https://github.com/settings/emails ” 这

  • 问题内容: 如果我想抓取一个需要先使用密码登录的网站,我该如何使用beautifulsoup4库开始使用python抓取它?以下是我对不需要登录的网站的处理方式。 应该如何更改代码以适应登录?假设我要抓取的网站是一个需要登录的论坛。一个示例是http://forum.arduino.cc/index.php 问题答案: 您可以使用机械化: 或urllib-使用urllib2登录网站

  • 问题内容: 我在爬网一个想要爬网的坚定网站时遇到了麻烦。问题是:成功登录该网站后,我无法访问需要有效登录的链接。 例如: 我在这里做的是: 从登录页面获取cookie,以便我可以正确登录; 然后,我发布到登录验证URL,该URL在登录后返回主页。 最终,我尝试在登录主页后尝试访问需要登录的URL,但是该请求使我返回登录页面,就好像会话已过期。 我知道我必须存储cookie才能使会话保持活动状态,但

  • 问题内容: 我想要做的是打开一个页面(例如youtube)并自动登录,就像我在浏览器中手动打开它一样。 据我了解,我必须使用cookie,问题是我不知道如何使用。 我尝试使用以下方法下载YouTube Cookie: 我得到的是: {'name':'VISITOR_INFO1_LIVE','value':'EDkAwwhbDKQ','path':'/','domain':'.youtube.com

  • 问题内容: 我正在尝试使用Python登录到此页面。 我尝试使用另一篇文章中描述的步骤,并获得了以下代码: 但这给了我以下输出: 我究竟做错了什么? 问题答案: 我建议使用精彩的模块。 下面的代码将使您登录到该站点,并在会话期间将cookie保留下来。

  • 本文向大家介绍使用python登录Selenium的重要性是什么?,包括了使用python登录Selenium的重要性是什么?的使用技巧和注意事项,需要的朋友参考一下 在Selenium中构建测试用例时,我们需要在框架中实现日志记录功能。这对于监控程序的流程至关重要,然后包括我们可能已经错过的其他边缘方案。 通过记录先前的测试步骤执行状态和详细信息,当出现错误时,日志可以提供比堆栈跟踪更多的信息。