Python 使用Splinter模块抓取动态内容
我正在努力抓取一个通过js动态加载的表(来自steamcommunity)。我使用的是python Splinter和无头浏览器Phantomjs的组合 以下是我已经想到的:Python 使用Splinter模块抓取动态内容,python,web-scraping,phantomjs,splinter,Python,Web Scraping,Phantomjs,Splinter,我正在努力抓取一个通过js动态加载的表(来自steamcommunity)。我使用的是python Splinter和无头浏览器Phantomjs的组合 以下是我已经想到的: from splinter import Browser import time import sys browser = Browser('phantomjs') url = 'https://steamcommunity.com/market/listings/730/%E2%98%85%20Karambit%20
from splinter import Browser
import time
import sys
browser = Browser('phantomjs')
url = 'https://steamcommunity.com/market/listings/730/%E2%98%85%20Karambit%20%7C%20Blue%20Steel%20(Battle-Scarred)'
browser.visit(url)
print browser.is_element_present_by_xpath('//*[@id="market_commodity_buyreqeusts_table"]', wait_time = 5)
price_table = browser.find_by_xpath('//*[@id="market_commodity_buyreqeusts_table"]/table/tbody/tr')
print price_table
print price_table.first
print price_table.first.text
print price_table.first.value
browser.quit()
第一个方法是\u xpath()提供的\u元素\u present\u,确保加载我感兴趣的表。然后我尝试访问该表的行
正如我从Splinter文档中了解到的那样,.find\u by_xpath()
方法返回ElementList
,它本质上是一个普通的列表,提供了一些别名
Price\u table
是表中所有行的元素列表。最后两个打印结果为空,我找不到text方法返回空字符串的任何原因
如何访问该表的元素?您是否尝试过在价格表中为i执行?从中,它声明ElementList
元素扩展了pythonlist
。我相信您可以在price\u表
上迭代
编辑:这也是我第一次听说splinter
,看起来它只是selenium
python包的一个抽象。也许如果你被卡住了,你可以看看。它们写得更好
from splinter import Browser
import time
import sys
browser = Browser('phantomjs')
url = 'https://steamcommunity.com/market/listings/730/%E2%98%85%20Karambit%20%7C%20Blue%20Steel%20(Battle-Scarred)'
browser.visit(url)
print browser.is_element_present_by_xpath('//*[@id="market_commodity_buyreqeusts_table"]', wait_time = 5)
price_table = browser.find_by_xpath('//*[@id="market_commodity_buyreqeusts_table"]/table/tbody/tr')
for i in price_table:
print i
print i.text
browser.quit()
我尝试使用不同的浏览器编写代码,结果总是空的文本
,但我在html
中找到了预期的数据。也许这只是斯普林特的一个错误
from splinter import Browser
#browser = Browser('firefox')
#browser = Browser('phantomjs')
#browser = Browser('chrome') # executable_path='/usr/bin/chromium-browser' ??? error !!!
browser = Browser('chrome') # executable_path='/usr/bin/chromedriver' OK
url = 'https://steamcommunity.com/market/listings/730/%E2%98%85%20Karambit%20%7C%20Blue%20Steel%20(Battle-Scarred)'
browser.visit(url)
print(browser.is_element_present_by_xpath('//*[@id="market_commodity_buyreqeusts_table"]', wait_time = 5))
price_table = browser.find_by_xpath('//*[@id="market_commodity_buyreqeusts_table"]/table/tbody/tr')
for row in price_table:
print('row html:', row.html)
print('row text:', row.text) # empty ???
for col in row.find_by_tag('td'):
print(' col html:', col.html)
print(' col text:', col.text) # empty ???
browser.quit()
非常感谢。我将使用.html方法提取数据。你认为使用selenium
而不是splinter
会更容易吗?我从来没有使用过splinter
,我只是偶尔使用selenium
,但它们看起来很相似。Selenium可能在互联网上有更多的信息/教程。我面临着组合的稳定性问题splitter
+phantomjs
。当它刮取1-10个链接时效果很好,但当我给它30个链接时,它总是在24-25个链接的某个页面上找不到表。你知道怎么处理吗?或者我应该切换到selenium
?使用所有信息创建新问题-错误消息、URL等。但是首先检查SO和Google,可能有人以前也遇到过同样的问题。在第24页到第25页看到的第一个HTML——可能没有表格,或者服务器发送了一些关于bot和captcha的消息。或者您必须等待JavaScript生成的数据。等