首页 > 【求助】python 遇到采集、正则问题,一直获取不到内容!

【求助】python 遇到采集、正则问题,一直获取不到内容!

要采集的地址是:http://wsgh.nbws.gov.cn/PlanList.shtml
PYTHON代码如下:

res=re.findall('<td style=\"width:80px;\">2015-10-26</td><td style=\"width:170px;\">(.*?)</td>',html,re.S);
print res

html代码如下:

<table class="GridView_General" cellspacing="0" cellpadding="0" border="0" id="GridViewReg" style="width:100%;border-collapse:collapse;table-layout:fixed">
                <tr class="GridView_SelectedRow">
                    <td style="width:45px;">1</td><td style="width:80px;">2015-10-26</td><td style="width:170px;">中医院</td><td style="width:100px;">中医</td><td style="width:50px;"><a href="/DoctorBrief/41952988403.shtml" target="_blank" style="color:Blue;text-decoration:underline;">医生</a></td><td style="width:65px;">周一</td><td style="width:45px;">0</td><td style="width:45px;">21</td><td style="width:55px;">20</td><td style="color:Green;width:55px;">1</td><td align="center" style="width:40px;"><input type="submit" name="GridViewReg$ctl02$btnReg" value="预约" id="GridViewReg_ctl02_btnReg" class="btn3_mouseout" onmouseover="this.className='btn3_mouseover'" onmouseout="this.className='btn3_mouseout'" onmousedown="this.className='btn3_mousedown'" onmouseup="this.className='btn3_mouseup'" style="width:40px;" />
                                                                                </td>
                </tr><tr class="GridView_AlternatingRow">
                    <td style="width:45px;">2</td><td style="width:80px;">2015-10-26</td><td style="width:170px;">中医院</td><td style="width:100px;">中医</td><td style="width:50px;"><a href="/DoctorBrief/41952988403.shtml" target="_blank" style="color:Blue;text-decoration:underline;">医生</a></td><td style="width:65px;">周一</td><td style="width:45px;">0</td><td style="width:45px;">21</td><td style="width:55px;">20</td><td style="color:Green;width:55px;">0</td><td align="center" style="width:40px;"><input type="submit" name="GridViewReg$ctl03$btnReg" value="预约" id="GridViewReg_ctl03_btnReg" disabled="disabled" class="btn3_mouseout" onmouseover="this.className='btn3_mouseover'" onmouseout="this.className='btn3_mouseout'" onmousedown="this.className='btn3_mousedown'" onmouseup="this.className='btn3_mouseup'" style="width:40px;" />
                                                                                </td>
                </tr><tr class="GridView_Row">
                    <td style="width:45px;">3</td><td style="width:80px;">2015-10-28</td><td style="width:170px;">中医院</td><td style="width:100px;">中医</td><td style="width:50px;"><a href="/DoctorBrief/41952988403.shtml" target="_blank" style="color:Blue;text-decoration:underline;">医生</a></td><td style="width:65px;">周三</td><td style="width:45px;">0</td><td style="width:45px;">60</td><td style="width:55px;">10</td><td style="color:Green;width:55px;">0</td><td align="center" style="width:40px;"><input type="submit" name="GridViewReg$ctl04$btnReg" value="预约" id="GridViewReg_ctl04_btnReg" disabled="disabled" class="btn3_mouseout" onmouseover="this.className='btn3_mouseover'" onmouseout="this.className='btn3_mouseout'" onmousedown="this.className='btn3_mousedown'" onmouseup="this.className='btn3_mouseup'" style="width:40px;" />
                                                                                </td>
                </tr><tr class="GridView_AlternatingRow">
                    <td style="width:45px;">4</td><td style="width:80px;">2015-10-29</td><td style="width:170px;">中医院</td><td style="width:100px;">中医</td><td style="width:50px;"><a href="/DoctorBrief/41952988403.shtml" target="_blank" style="color:Blue;text-decoration:underline;">医生</a></td><td style="width:65px;">周四</td><td style="width:45px;">0</td><td style="width:45px;">21</td><td style="width:55px;">20</td><td style="color:Green;width:55px;">0</td><td align="center" style="width:40px;"><input type="submit" name="GridViewReg$ctl05$btnReg" value="预约" id="GridViewReg_ctl05_btnReg" disabled="disabled" class="btn3_mouseout" onmouseover="this.className='btn3_mouseover'" onmouseout="this.className='btn3_mouseout'" onmousedown="this.className='btn3_mousedown'" onmouseup="this.className='btn3_mouseup'" style="width:40px;" />
                                                                                </td>
                </tr>
            </table>

你正则里面的日期是固定的, 但你发的网址, 目前上面的数据的日期已经没有 2015-10-26 的数据了.
我刚试了一下你提供的正则和你提供的数据是可以匹配出来内容的.

但如果你那个是去获取的那个URL的内容, 应该就匹配不到了, 因为没有那个日期的数据.


一模一样的正则..

附完整代码(文件保存为UTF-8编码):

#!/usr/bin/python
# -*- coding=utf-8 -*-
import urllib, urllib2, cookielib, re, sys

reload(sys)
sys.setdefaultencoding('utf-8')

__cookie = cookielib.CookieJar()
__req = urllib2.build_opener(urllib2.HTTPCookieProcessor(__cookie))
__req.addheaders = [
  ('Accept', 'application/javascript, */*;q=0.8'),
  ('User-Agent', 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)')
]

urllib2.install_opener(__req)


request = urllib2.Request('http://wsgh.nbws.gov.cn/PlanList.shtml')
html = urllib2.urlopen(request).read()


res=re.findall('<td style=\"width:80px;\">(.*?)</td><td style=\"width:170px;\">(.*?)</td>', html, re.S);
for item in res:
  print "%s %s\n" % (item[0], item[1].encode('utf-8'))
【热门文章】
【热门文章】