【问题标题】:BeautifulSoup empty resultBeautifulSoup 空结果
【发布时间】:2020-02-28 18:02:00
【问题描述】:

我目前正在运行此代码:

    import urllib
    from bs4 import BeautifulSoup

    htmltext = urllib.urlopen("http://www.fifacoin.com/")
    html = htmltext.read()

    soup = BeautifulSoup(html)
    for item in soup.find_all('tr', {'data-price': True}):
        print(item['data-price'])

当我运行此代码时,我根本没有得到任何输出,当我知道该特定网站上有带有这些搜索参数的 html 标记时。我可能在这里犯了一个明显的错误,我是 Python 和 BeautifulSoup 的新手。

【问题讨论】:

标签: python web-scraping beautifulsoup


【解决方案1】:

问题是价目表是通过javascript加载的,据我所知,urllib不包含任何javascript引擎。因此,在普通浏览器中执行的该页面中的所有 javascript 都不会在 urllib 获取的页面中执行。 这样做的唯一方法是模拟真实的浏览器。 想到的解决方案是 PhantomJS 和 Node.js。

我最近用 nodejs 做了一个类似的事情(虽然我也是一个 python 粉丝)并且现在很惊讶。我做的有点不同,但这个页面似乎很好地解释了你想要做什么:http://liamkaufman.com/blog/2012/03/08/scraping-web-pages-with-jquery-nodejs-and-jsdom/

【讨论】:

    猜你喜欢
    • 2017-03-05
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 2020-04-23
    • 2012-12-15
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    相关资源
    最近更新 更多