找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 2260|回复: 1

[求助] 为什么获取网页源代码为空??

2

主题

3

帖子

3

积分

贫民

积分
3
happykjoy 发表于 2018-9-20 10:05:19 | 显示全部楼层 |阅读模式
代码:

# -*- coding:utf-8 -*-
import requests
import json

def get_one_page(url,headers):

    resp**e = requests.get(url,headers)
    print(resp**e.cookies)
    print(resp**e.status_code)
    print(resp**e.text)
    print(type(resp**e.text))
    print(resp**e.json)
    print(type(resp**e.json))

if __name__ == '__main__':
    headers={
        'accept': 'text / html, application / xhtml + xml, application / xml;q = 0.9, image / webp, image / apng, * / *;q = 0.8',
        'accept - encoding': 'gzip, deflate',
        'accept - language': 'zh - CN',
        'cache - control': 'max - age = 0',
        'dnt': '1',
        'upgrade - insecure - requests': '1',
        'user - agent': 'Mozilla / 5.0(Windows NT 6.1) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 61.0.3163.79 Safari / 537.36 Maxthon / 5.2.3.3000',
        'x - devtools - emulate - network - conditi** - client - id': '0f286fdf - ae53 - 4784 - 9610 - 56f5b068a872'
    }
    url = 'https://www.toutiao.com/a6602782094278001159/'
    get_one_page(url,headers)


运行结果:
C:\Users\Administrator\PycharmProjects\requestsprider\venv\Scripts\python.exe C:/Users/Administrator/PycharmProjects/requestsprider/venv/test.py
<RequestsCookieJar[]>
200
<!DOCTYPE html><html lang=en><head><meta charset=UTF-8><meta name=viewport content="width=device-width,initial-scale=1"><meta http-equiv=X-UA-Compatible content="ie=edge"><link rel="shortcut icon" href=//s3a.pstatp.com/toutiao/resource/ntoutiao_web/static/image/favicon_8e9c9c7.ico type=image/x-icon><title>今日头条</title></head><body><p></p></body></html>
<class 'str'>
<bound method Resp**e.json of <Resp**e [200]>>
<class 'method'>

Process finished with exit code 0


这个是为什么??网页源代码为什么为空?高手帮看一下。谢谢!
回复

使用道具 举报

5

主题

13

帖子

13

积分

贫民

积分
13
ssaxi 发表于 2018-10-6 08:58:30 | 显示全部楼层
我也是新手。不太懂。
改了一下,获取内容比你的多了点
hd={'user-agent':'Chrome/10'}
url = 'https://www.toutiao.com/a6602782094278001159/'
resp = requests.get(url,headers=hd)
resp.encoding=resp.apparent_encoding
回复 支持 反对

使用道具 举报

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

本版积分规则

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