我在Python上使用beautifulsoup废弃了this website。 我可以在Chrome上看到html代码:
<td id="2412_b4" align="center" style="color: rgb(16, 80, 16);">97.40</td>
当我使用Python获取文本内部时 我得到了这样的结果:
<td align="center" id="2412_b4">-</td>
我的代码是
# -*- coding: utf-8 -*-
import urllib2, sys
#from BeautifulSoup import beautifulsoup
from bs4 import BeautifulSoup
from HTMLParser import HTMLParser
import requests
#from lxml import html
site= "http://mis.twse.com.tw/stock/fibest.jsp?stock=2412"
hdr = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36',
'Accept-Language':'zh_tw',
'Cookie':'JSESSIONID=761ED2DA50CCC87E053877524B1827D7',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Cache-Control':'max-age=0',
'Accept-Encoding':'gzip, deflate, sdch'
}
req = requests.get(site, headers = hdr, verify = False)
#page = urllib2.urlopen(req)
soup = BeautifulSoup(req.text,"html.parser")
T = soup.findAll('tbody', {'id':'hor-minimalist-tb'})
for name in T:
print name.text
请告诉我如何获取文本内容 谢谢
=============================================== ================
我尝试了@Anand S Kumar给我的教程。 我编辑下面的代码:
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.QtWebKit import *
from lxml import html
#Take this class for granted.Just use result of rendering.
class Render(QWebPage):
def __init__(self, url):
self.app = QApplication(sys.argv)
QWebPage.__init__(self)
self.loadFinished.connect(self._loadFinished)
self.mainFrame().load(QUrl(url))
self.app.exec_()
def _loadFinished(self, result):
self.frame = self.mainFrame()
self.app.quit()
url = "http://mis.twse.com.tw/stock/fibest.jsp?stock=2412"
#url = 'http://pycoders.com/archive/'
r = Render(url)
result = r.frame.toHtml()
#QString should be converted to string before processed by lxml
formatted_result = str(result.toAscii())
#Next build lxml tree from formatted_result
tree = html.fromstring(formatted_result)
#Now using correct Xpath we are fetching URL of archives
archive_links = tree.xpath('//tbody[@id="hor-minimalist-tb"]/tr/td/text()')
print archive_links
它仍然不适合我。里面的价值仍然是 -
答案 0 :(得分:1)
就像在另一个答案中已经说过的那样,页面中的实际数据是javascript渲染/ ajax渲染。
如果您在页面的chrome中使用view page source
,您将看到要检查的组件的原始html源代码 -
<tbody id="hor-minimalist-tb">
<tr>
<td align="center" style="color:#000000">-</td>
<td align="center">-</td>
<td id="2412_a4" align="center">-</td>
<td id="2412_f4" align="center" style="color:#000000">-</td>
</tr>
<tr>
<td align="center" style="color:#000000">-</td>
<td align="center">-</td>
<td id="2412_a3" align="center">-</td>
<td id="2412_f3" align="center" style="color:#000000">-</td>
</tr>
<tr>
<td align="center" style="color:#000000">-</td>
<td align="center">-</td>
<td id="2412_a2" align="center">-</td>
<td id="2412_f2" align="center" style="color:#000000">-</td>
</tr>
.
.
</tbody>
当你这样做时 -
req = requests.get(site, headers = hdr, verify = False)
req.text
包含页面的原始html源代码,而不是javascript呈现的DOM。这就是为什么你得到BeautifulSoup的结果。
对于javascript呈现的页面,您可以按照教程 - https://impythonist.wordpress.com/2015/01/06/ultimate-guide-for-scraping-javascript-rendered-web-pages/(有关lxml的代码,但可以很容易地适用于BeautifulSoup)。它使用QT Web Kit库。 (免责声明:这不是我的教程/内容)
答案 1 :(得分:0)
真实数据是通过Ajax加载的,这就是为什么BeautifulSoup只能获得占位符。
要监控ajax请求,您可以使用Chrome开发者工具等工具。
答案 2 :(得分:0)
如果喜欢这种情况,数据不在HTML源代码中,它应该来自某个地方。因此,如果您检查站点发出的HTTP连接,您会发现正在从网站API获取数据:
http://mis.twse.com.tw/stock/api/getStockInfo.jsp?ex_ch=tse_2412.tw&json=1&delay=0&_=1438929024846
json的例子:
{
"msgArray": [
{
"ts": "0",
"tk0": "2412.tw_tse_20150807_B_9999301124",
"tk1": "2412.tw_tse_20150807_B_9999283564",
"oa": "98.00",
"ob": "97.90",
"tlong": "1438928700000",
"f": "1584_141_122_674_83_",
"ot": "14:25:00",
"ex": "tse",
"g": "22_130_76_108_106_",
"ov": "-",
.
.
.
.
}
但是,当您想要直接访问URL时。您需要使用有效的cookie。但如果你试图
s = requests.Session()
r = s.get("http://mis.twse.com.tw/stock/fibest.jsp?stock=2412")
你会得到<Response [500]>
通过检查响应代码,您会发现需要发送Accept-Language
个Cookie。
r = s.get("http://mis.twse.com.tw/stock/fibest.jsp?stock=2412", headers = {"Accept-Language":"en-US,en;q=0.5"})