python学习Day1

成品展示

在这里插入图片描述

0、准备工作

0.1 Pycharm安装及配置
0.2 Python安装及环境配置
0.3 pip安装与使用 (选择性安装Python 2.7.9 + 或 Python 3.4+ 以上版本都自带 pip 工具。)

1、Python语法

之前有过其他编程语法经验的铁子大可不必从零开始,Python的语法要简单很多,建议直接看完整的demo,遇到难点再针对性查看,要省时不少

Python大佬的完整demo
Python基础教程|菜鸟教程

2、摸索实践

2.1 观察页面

打开知乎热搜 ->用F12打开控制台 ->鼠标点击红色框体 ->鼠标移动到黑色框体处 ->观察控制台蓝色框体的结构

在这里插入图片描述

2.2 分析结构

经过简单观察,可以发现热搜内容储存在 id=‘TopstoryContent’的div标签下的a标签里,所以只要取到a标签里的内容即可

在这里插入图片描述

2.3 代码编写

根据上一步观察出来的结构,开始大致的代码编写

import requests
from bs4 import BeautifulSoup
url = 'http://www.zhihu.com/hot'  # 知乎首页网址
resp = requests.get(url)  # 获取此网址的所有内容
html = resp.content  # 获取网址的有效内容
html = str(html, 'utf-8')  # 转utf8,解决汉字乱码
# 拿到id=TopstoryContent的div标签下的所有a标签
bf = BeautifulSoup(html, 'html.parser')
hop = bf.find('div', id="TopstoryContent").find_all('a')
# 打印hop内数据
print(hop)
2.4 绕过反爬及登录

报错:'NoneType' object has no attribute 'find_all'
在这里插入图片描述
打断点发现获取网址内容返回403,说明知乎有反爬和登录设置,查阅资料,有多种解决方法,这里就先简单只取给请求加header和cookie解决(header和cookie从Network方法里Header的RequestHeader内复制

import requests
from bs4 import BeautifulSoup
cookie = '''xxx'''
headers = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
                       "Chrome/80.0.3987.132 Safari/537.36","Cookie": cookie}
url = 'http://www.zhihu.com/hot'  # 知乎首页网址
resp = requests.get(url, headers=headers)  # 获取此网址的所有内容
html = resp.content  # 获取网址的有效内容
html = str(html, 'utf-8')  # 转utf8,解决汉字乱码
# 拿到id=TopstoryContent的div标签下的所有a标签
bf = BeautifulSoup(html, 'html.parser')
hop = bf.find('div', id="TopstoryContent").find_all('a')
print(hop)

再次运行,没有出现问题

3、核心功能实现

3.1创建txt文件
#循环所有a标签,拿到a标签中的内容(热搜名称)
for src in hop:
    try:
        #获取热搜标题
        title = src.get('title')
        
        # 跳转进热搜内链接,获取热搜内容,与获取标题同理
        goto = src.get('href')
        rep = requests.get(goto, headers=headers)
        contents = str(rep.content, 'utf-8')
        contentBF = BeautifulSoup(contents, 'html.parser')
        text = contentBF.find('span', itemprop='text')
        content = text.contents[0]
        
        
        #将热搜名保存为文件名
        chapter = save_path + "/" + str(title) + ".txt"
        #将热搜内容循环填入txt文件中
        with codecs.open(chapter, 'a', encoding='utf-8') as f:
            f.write(str(text))
        print(content)
    except Exception as e:
        print(e)

运行测试,内容取到了,但没有完全取到...
在这里插入图片描述

3.2待解决问题:

3.2.1 知乎内容不会一次请求全部显示,需要点击“显示全部”之后,完整内容才会显示,只用python无法完成爬取时点击按钮功能,需要用Selenium辅助实现,但是我不会

在这里插入图片描述

3.2.2 经过仔细研究知乎的网页结构,发现在热搜首页是有完整的内容显示的!!比最初设想的方法更简单!获取知乎热搜顺利解决!但是点击全部获取内容方式的问题还没有解决
在这里插入图片描述

3.3完整代码
import requests
from bs4 import BeautifulSoup


import requests
from bs4 import BeautifulSoup
import codecs

# 定义存储位置
global save_path
#热搜下载储存地址
save_path = 'E:/PythonTest/eg1/zhihuTop'

cookie = '''自己的cookie复制在这里'''
headers = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
                       "Chrome/80.0.3987.132 Safari/537.36","Cookie": cookie}
url = 'https://www.zhihu.com/hot'
resp = requests.get(url, headers=headers)
html = resp.content
html = str(html, 'utf-8')
bf = BeautifulSoup(html, 'html.parser')
hop = bf.find('div', id="TopstoryContent").find_all('a')
for src in hop:
       try:
        title = src.get('title')
        if(src.contents[1]):
         hopContent = src.contents[1].contents
         chapter = save_path + "/" + str(title) + ".txt"
         with codecs.open(chapter, 'a', encoding='utf-8') as f:
             f.write(str(hopContent))
    except Exception as e:
        print(e)

最后

因为新学python,许多方法还是不熟,代码格式可能欠规范,有错误希望评论提出,共同进步
这里只是拿知乎例子进行举例,所有网站的热搜或者内容均可同理解决

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值