鱼C论坛

 找回密码
 立即注册
查看: 1128|回复: 4

爬取html信息求助

[复制链接]
发表于 2019-7-16 16:04:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
想要从网页里提取想要的部门和它的时间信息
  1. import urllib.request
  2. import urllib.parse

  3. headers = {
  4.    'Accept': 'image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/xaml+xml, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*',
  5.    'Connection': 'Keep-Alive',
  6.    'Content-Length': '60',
  7.    'Content-Type': 'application/x-www-form-urlencoded',
  8.    'Cookie': 'ASPSESSIONIDCCTCTDBD=NLHPINNBKJPPOLILHDBLEKNN',
  9.    'Host': '10.1.6.10:5700',
  10.    'Referer': 'http://10.1.6.10:5700/Worker_Stat/ReturnDep_Stat.asp',
  11.    'User-Agent': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; InfoPath.3)'
  12. }
  13. data = {}
  14. data['ST']='2019-6-1'
  15. data['ET']='2019-6-3'
  16. data['DepID']='262'
  17. data['button'] = '%E7%BB%9F%E8%AE%A1'
  18. data = urllib.parse.urlencode(data).encode('utf-8')
  19. url = 'http://10.1.6.10:5700/Worker_Stat/ReturnDep_Stat.asp'
  20. r = urllib.request.Request(url,headers=headers)
  21. res = urllib.request.urlopen(r,data)
  22. t = res.read()
  23. t = t.decode('utf-8')
  24. print(t)
复制代码

获得的html中没有请求得到的部门和时间
下面是输出结果
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>维修回笼统计(部门)</title>

  6. <link href="../css/table.css" rel="stylesheet" type="text/css" />
  7. <script language=javascript src=../month.js></script>
  8. <script language=javascript src=../com/func.js></script>
  9. <script language=javascript>
  10. function showdiv() {
  11. divWait.style.pixelTop = document.body.offsetHeight / 2 - parseInt(divWait.style.height) / 2+77;
  12. //divWait.style.pixelTop =document.body.clientHeight/ 2 - parseInt(divWait.style.height) / 2+77;

  13. divWait.style.pixelLeft = document.body.offsetWidth / 2 - parseInt(divWait.style.width) / 2;
  14. divWait.style.visibility = "visible";
  15. }
  16. function hidediv() {
  17. divWait.style.visibility = "hidden";
  18. }

  19. function showdiv2() {
  20. divWait2.style.pixelTop = document.body.offsetHeight / 2 - parseInt(divWait.style.height) / 2+77;
  21. divWait2.style.pixelLeft = document.body.offsetWidth / 2 - parseInt(divWait.style.width) / 2;
  22. divWait2.style.display = "";
  23. }
  24. </script>
  25. <DIV id=divWait style="visibility: hidden; WIDTH: 200px; POSITION: absolute; HEIGHT: 19px; BACKGROUND-COLOR: #ffffa3; fontSize: 10pt" align=center VALIGN="center">
  26. <table border=1 width="100%" style="border-collapse:collapse;">
  27. <tr style="BACKGROUND-COLOR: #ffffa3;"><td align=center ><img align=absmiddle src=../style/waitdiv3.gif>&nbsp;&nbsp;<b>数据查询中,请稍候……</b></td>
  28. </tr></table>
  29. </DIV>

  30. <DIV id=divWait2 style="display: none; WIDTH: 200px; POSITION: absolute; HEIGHT: 19px; BACKGROUND-COLOR: #ffffa3; fontSize: 10pt" align=center VALIGN="center">
  31. <table border=1 width="100%" style="border-collapse:collapse;">
  32. <tr style="BACKGROUND-COLOR: #ffffa3;"><td align=center ><img align=absmiddle src=../style/waitdiv3.gif>&nbsp;&nbsp;<b>数据查询中,请稍候……</b></td>
  33. </tr></table>
  34. </DIV>
  35. <style type="text/css">
  36. .style1 {
  37.         background-color: #E8E6E6;
  38. }
  39. </style>

  40. </head>

  41. <body>

  42. <form method="post" name="NmssForm" action="ReturnDep_Stat.asp" style="margin:0px">
  43. <table width="100%" border="0" cellspacing="0" cellpadding="0" class="search">
  44.   <tr>
  45.     <td width="5" align="center"><!--<img src="../images/search2.gif" />--></td>
  46.     <td width="70" class="cx">统计条件</td>
  47.     <td width="270" >&nbsp;报修日期&nbsp;<input name="ST" type="text" class="searchinput" value="2019-6-1" onclick="javascript:fPopCalendar(ST,ST);return false;" />
  48. 至&nbsp;<input name="ET" type="text" class="searchinput" value="2019-6-3" onclick="javascript:fPopCalendar(ET,ET);return false;" />
  49.         </td>
  50.         <td>部门 <select name='DepID' class='searchxl'><option></option></select></td>
  51.         <td width="200"></td>
  52.     <td>
  53.       <label>
  54.         <input type="submit" name="button" value="统计" class="searchbtn"  onclick="showdiv();" />
  55.         </label>
  56.     </td>
  57.     </tr>

  58. </table>
  59. </form>

  60. <div class="listtitle"><b> 部门维修回笼统计表</b>2019-6-1 — 2019-6-3</div>
  61. <ul class="btnlist">
  62.     <li><a onclick="window.open('ReturnDep_StatExcel.asp?community=&Worker=&ST=2019-6-1&ET=2019-6-3&Worked=&ServAreaID=&orderUp=&C1=2&DepID=262')" style="cursor:hand"><span><img src="../images/btn02.gif" alt="导出到Excel" /></span>导出到Excel</a></li>
  63.     </ul>
  64. <div class="box"></div>

  65. <center>
  66. <table width="80%" border="0" cellspacing="0" cellpadding="0"  class="ntable">
  67. <tr>
  68.                 <td ><b>部门</td>
  69.                 <td ><b>维修数量</td>
  70.                 <td ><b>平均回笼时间</td>
  71.         </tr>

  72. <tr >
  73.                 <td ><b>总计</td>
  74.                 <td ><b>&nbsp;</td>
  75.                 <td ><b>&nbsp;</td>
  76.         </tr>
  77. </table>

  78. <div class="box"></div>
  79. <div class="box">* 维修回笼时间:业务从派单到维修完成的时长</div>
  80. </center>
  81. </body>
  82. </html>
复制代码

请求参数:

请求参数

请求参数

正确获得的html内容:

需要的html

需要的html

网页界面:

网页界面

网页界面


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-7-16 16:40:42 | 显示全部楼层
是内网ip吗  那没办法帮你测试啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-7-16 16:41:33 | 显示全部楼层
wp231957 发表于 2019-7-16 16:40
是内网ip吗  那没办法帮你测试啊

是哎,头疼
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-16 16:44:47 | 显示全部楼层
本帖最后由 wp231957 于 2019-7-16 16:55 编辑


建议你使用requests+lxml  试一下,不要用urllib
目测你的网站(网页) 应该没有加密,所以 你要的信息还是很好提取的

REQUESTS+LXML
REQUESTS+RE
REQUESTS+BS4   

看你对哪个组合比较熟悉
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-21 00:24:00 | 显示全部楼层
这里的内容是动态js加载,直接抓api的内容就好了。
requests 和json配合好就行了。

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-16 23:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表