【问题标题】:How to parse a site that returns a different HTML <title>Please wait 5 seconds...</title>如何解析返回不同 HTML <title>请等待 5 秒...</title> 的网站
【发布时间】:2016-02-04 21:11:15
【问题描述】:
import requests
from bs4 import BeautifulSoup

URL="https://kissanime.to"
page = requests.get(URL)

Code = BeautifulSoup(page.content,"html.parser")
print Code.title

这是输出

<title>Please wait 5 seconds...</title>

每次我从这个网站请求这是我唯一得到的东西。有没有办法解决这个问题并从实际站点获取 HTML 代码?

我想得到:

<title>KissAnime - Watch anime online in high quality</title>

【问题讨论】:

    标签: python python-2.7 web-scraping request


    【解决方案1】:

    这个特定的网站是非常动态的,它需要一个真正的浏览器来加载。让我们通过selenium WebDriver 控制PhantomJS headless browser,加载页面和wait 的标题不是等于“请等待 5 秒...”:

    from selenium import webdriver
    from selenium.webdriver.support.wait import WebDriverWait
    
    driver = webdriver.PhantomJS()
    driver.get("https://kissanime.to")
    
    # 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...")
    
    print(driver.title)
    

    打印:

    KissAnime - Watch anime online in high quality
    

    【讨论】:

    • 我下载了 selenium 和 PhantomJS,但我不断收到路径错误,我无法弄清楚如何修复。 WebDriverException:消息:“phantomjs”可执行文件需要在 PATH 中。我应该把phantomjs放在哪里?
    • @forest203 好吧,这肯定超出了这个特定主题的范围。如果您无法自行解决或在网上找到解决方案,请查看在 SO 上创建单独的问题是否有意义。感谢理解。
    • path=r'C:\Users\Forest\Desktop\New folder\phantomjs-2.1.1-windows\bin\phantomjs.exe' driver.PhantomJS(executable_path=path) 不认识你在路径之前需要一个 r
    • 我想知道当我运行这个时是否有可能不显示 phantomJS 黑框?
    • @forest203 有趣的问题,这里有一个 C# 特定的解决方案:stackoverflow.com/questions/20711407/…。看看创建一个关于它的 python+selenium+phantomjs 特定问题是否有意义。谢谢。
    猜你喜欢
    • 2013-03-20
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 2011-10-05
    相关资源
    最近更新 更多