鱼C论坛

 找回密码
 立即注册
查看: 2222|回复: 1

爬墨迹天气 response.text显示出来的html网页不完整

[复制链接]
发表于 2018-4-16 13:26:55 | 显示全部楼层 |阅读模式

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

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

x

大神们好

小弟在尝试爬取墨迹天气
代码如下:

  1. import requests
  2. from bs4 import BeautifulSoup
  3. import sys
  4. sys.path.append('C:\Python27\Lib\site-packages')

  5. def open_url():
  6.     url = 'https://tianqi.moji.com/weather/china/guangdong/tianhe-district'
  7.     headers = {
  8.         'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)',
  9.         'Host': 'httpbin.org'
  10.     }
  11.     response = requests.get(url, headers = headers)
  12.     print(response.text)


  13. open_url()
复制代码



运行打印出来的reponse.text的内容只有下面一点点, 原网页的html内容远远不止这些, 请问是我步骤哪里错了吗?

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>&#230;&#156;&#141;&#229;&#138;&#161;&#229;&#153;¨&#231;1&#129;&#229;&#191;&#153;</title>
  6. <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0">
  7. <meta content="yes" name="apple-touch-fullscreen"/>
  8. <meta name="format-detection" content="telephone=no" />
  9. <meta name="author" content="mojichina" />
  10. <style>
  11. /*css reset*/
  12. body, firgue { margin:0; }
  13. body { font:normal normal 1em/1 'Helvetica Neue', Helvetica, Arial, sans-serif; }
  14. a {outline:none;text-decoration:none; }
  15. a:hover {outline:none;text-decoration:none; }
  16. body {
  17.     background:#f4f4f4;
  18. }
  19. .queues {
  20.     max-width:640px;
  21.     min-width:320px;
  22.     margin:0 auto;
  23.     text-align:center;
  24. }
  25. .queues figure {
  26.     margin:0;
  27.     padding:30px 0 0 0;
  28.     width:100%;
  29.     text-align:center;
  30. }
  31. .queues figure img {
  32.     display:inline-block;
  33. }
  34. .queues div {
  35.     padding:42px 0 80px 0;
  36.     width:100%;
  37.     text-align:center;
  38.     color:#0097e0;
  39.     font-size:24px;
  40.     line-height:1;
  41.     font-weight:bold;
  42. }
  43. .queues a {
  44.     display:inline-block;
  45.     margin:0 auto;
  46.     width:190px;
  47.     height:40px;
  48.     color:#fff;
  49.     font-size:17px;
  50.     line-height:40px;
  51.     text-align:center;
  52.     background:#0097e0;
  53.     border-radius:5px;
  54. }
  55. </style>
  56. </head>
  57. <body>
  58. <div class="queues">
  59.     <figure>
  60.         <img src="http://cdn2.moji002.com/webpush/errimg/500.png" width="232" height="166">
  61.     </figure>   
  62.     <div>&#230;&#156;&#141;&#229;&#138;&#161;&#229;&#153;¨&#231;1&#129;&#229;&#191;&#153; èˉ·&#231;¨&#141;&#229;&#144;&#142;&#229;&#134;&#141;èˉ&#149;</div>
  63.     <a href="javascript:;"  onclick="history.go(-1)" >è&#191;&#148;&#229;&#155;&#158;&#228;&#184;&#138;&#228;&#184;&#128;é&#161;μ</a>
  64. </div>
  65. </body>
  66. </html>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-4-16 13:50:59 | 显示全部楼层
自己又解决了...

把headers改成

  1. headers = {
  2.         'User-Agent' : 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0',
  3.         'Content-Type': 'application/x-www-form-urlencoded',
  4.         'Connection' : 'Keep-Alive',
  5.         'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
  6. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-29 11:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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