以上为我写的,抓不到内容什么情况,但是在浏览器中能够看到里面的内容,不知道什么情况
文章的内容是动态加载的,不在这个链接。
import requests
from copyheaders import headers_raw_to_dict as hd #自定义的一个headers处理函数
import json
headerse = hd(b'''这里是头文件"''')
url = 'https://www.tadu.com/partContent/703748/8'
response = requests.get(url=url,headers=headerse )
print(response.status_code)
contentdata = json.loads(response.text)
print(contentdata["content"])
输出结果
这个界面使用了ajax技术,所以直接访问这个链接获取不到相关数据的,需要找到这个链接接口即可,如下:
参考代码如下:
import requests
url = 'https://tadu.com/partContent/703748/8'
headers = {
"accept": "*/*",
"accept-encoding": "gzip, deflate, br",
"accept-language": "zh-CN,zh;q=0.9",
"cookie": "_ebc81c6d435065d480cb865bdbc4fec2=a716beeb77831f65ae9f02780aeb40ad8c9e0a49ccbfeab2a89dfc532ad4526220c4774c38a1f1e70fd6188df466d7ad; _ee965b7158f5ac6ba1d18e3e6bdf7644=40518b8ad6a369e9d4f4a04866537056; font_size=; screen_width=",
"referer": "https://tadu.com/book/703748/83158692",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3877.400 QQBrowser/10.8.4507.400",
"x-requested-with": "XMLHttpRequest"
}
rsp = requests.get(url=url, headers=headers)
print(rsp.status_code)
print(rsp.text)
也可以去看看我的这些文章:https://liuze.blog.csdn.net/article/details/105965562,https://liuze.blog.csdn.net/article/details/126252928,虽然讲的不全面哈!
关于文件其实还有很多的方法,用到的时候可以去网上查查资料去深入了解,比如说os模块里面还有其他的方法,我们可以ctrl+鼠标左键进入模块内部查看,比如说不止os模块,还有os.path这个模块也是比较常用的文件操作模块。
那么今天的分享就先到这里,看完可以收藏起来多练练,文件操作也就不再困难了。
准备了很久,我的公众号终于是开始运作了,关于我的技术和经验文章都会优先在上面进行推送,目前还在不断完善中,欢迎大家的关注。