python - BS4 中出现奇怪的错误。 find_all() 返回 None

标签 python html web-scraping beautifulsoup phantomjs

我正在使用 BS4 和 PhantomJS 来抓取网站。在 Mac 上一切正常,但在 Windows 上我遇到了一个奇怪的错误:find_all() 返回 None,但元素存在!

我的代码:

def get_venues():
    driver = webdriver.PhantomJS(executable_path = path)
    url=web+'#/racing'
    driver.get(url)
    try:
        wait = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CLASS_NAME, "wrapper")))
    finally:
        content=driver.page_source
        soup=bs4.BeautifulSoup(content, "html5")
        driver.quit()

    b = soup.find(id='content').div
    print(b)
    c = b.ul(attrs={'class': 'main-list'})

    print(c)

cNone,不应该是 b 那样的情况:

</ul></div><ul class="main-list"><li><div class="collapsible R"><div class="icon race_code_R"></div><span>Thoroughbreds</span><div class="arrow_down_sign"></div></div><ul class="sub-list"><li class="venue cell"><a href="#/meetings/19197">
  <span class="location">Beaudesert</span>
  <div class="goto-sign"></div>
</a>
<a class="next-race" href="#/races/181880/exchange/win">
  <span class="time-left critical">-30m</span>
  <span class="number">R5</span>
</a>
</li><li class="venue cell"><a href="#/meetings/19199">
  <span class="location">Werribee</span>
  <div class="goto-sign"></div>
</a>
<a class="next-race" href="#/races/181900/exchange/win">
  <span class="time-left critical">-38s</span>
  <span class="number">R7</span>
</a>
</li><li c

最佳答案

将代码从 mac 转移到 windows 机器的问题是,在编码文件时使用略有不同的 utf-8 值,如果不被捕获,可能会破坏你的程序,所以我在这里最好的猜测(我不是 python 专家)是不是从你的 Mac 到你的 PC 的过程中,UTF-8 字符发生了变化,现在你的整个程序都被淘汰了,解决这个问题的一种方法是在基于 Windows 的 IDE/编译器/文本编辑器中从头开始重建它希望这有帮助

关于python - BS4 中出现奇怪的错误。 find_all() 返回 None,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27032991/

相关文章:

javascript - 以 Shopify 的购买按钮为中心的问题

html - Bootstrap 图标已停止出现在我的应用程序中

javascript - 使用 gulp 设置配置文件。站点加载但 css 注入(inject)没有发生。我该如何解决?

python-3.x - Scrapy - 类型错误 : 'Rule' object is not iterable

python - 在 python 中使用 getattr 的替代方法?

python - 如何在 Python 的 if 语句中使用 RegEx?

python - 使用 beautifulsoup 查找标题标签内的链接

python - 来自 Google Finance 的网络抓取 : returned data list always empty

python - 使用 Scapy 缩短 TCP 有效负载会导致 [TCP 上一段未捕获]

python - 一次获取 NumPy 数组中多行的索引