【发布时间】:2019-08-16 06:56:46
【问题描述】:
我是爬虫的新手。我正在尝试使用按钮立即购买从this site 中获取价值。
我尝试过的选项是:
from PyQt4.QtGui import QApplication
from PyQt4.QtCore import QUrl
from PyQt4.QtWebKit import QWebPage
class Client(QWebPage):
def __init__(self):
self.app = QApplication(sys.argv)
QWebPage.__init__(self)
# self.loadFinished.connect(self.on_page_load)
# self.mainFrame().load(QUrl(url))
# self.app.exec_()
def on_page_load(self):
self.app.quit()
def mypage(self, url):
self.loadFinished.connect(self.on_page_load)
self.mainFrame().load(QUrl(url))
self.app.exec_()
client_response = Client()
def parse(url): # OSRS + RS3
client_response.mypage(url)
source = client_response.mainFrame().toHtml()
soup = BeautifulSoup(source, 'html.parser')
osrs_text = soup.findAll('input', attrs={'type': 'number'})
quantity = (osrs_text[0])['min']
final = 0
if(quantity == '1'):
final_osrs = round(float(soup.findAll('span', attrs={'id':'goldprice'})[0].text),3)
print(final_osrs)
else:
price = round(float(soup.findAll('span', attrs={'id':'goldprice'})[0].text),3)
final_rs3 = price/int(quantity)
print(final_rs3)
这种方法不好,因为它需要太多时间来抓取。 我也尝试过 Selenium 方法,但目前也不需要。
你们能建议我更好的方法来获取价值吗? 。
任何帮助将不胜感激。谢谢。
P.S:我尝试了这个库,因为内容是动态生成的。
【问题讨论】:
-
对于一个新的贡献者来说,这是一个很好的问题。 + 1.记得通过edit使用sn-p工具插入html。优化问题也可能是 code review site 的候选问题 - 但请务必在发布之前阅读他们的指导。
标签: python python-3.x web web-scraping beautifulsoup