python3 爬取糗事百科
Python3 爬取 百科
2023-09-14 09:16:00 时间
1.准备:
python 3.6
需要用到的包:re request BeautiflSoup urllib
2.代码如下:
# -*- coding: utf-8 -*-
import urllib.request
import re
from urllib import request
from bs4 import BeautifulSoup
articleUrl = "https://www.qiushibaike.com/textnew/page/%d"
#段子地址
commentUrl = "https://www.qiushibaike.com/article/%s"
#评论
page = 0
Url = articleUrl % page
#1.获取url源码
def getContentOrComment(Url):
user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
headers = {'User-agent': user_agent} #浏览器信息
req = request.Request(url=Url, headers=headers)
response = urllib.request.urlopen(req) #打开网址
content = response.read().decode('utf-8') #读取所有源代码
#print(content)
return content
articlePage = getContentOrComment(Url)
#2.获取话题内容
soup = BeautifulSoup(articlePage, 'html.parser')
#print(soup) #格式化输出
floor = 1
#attrs属性
for string in soup.find_all(attrs="article block untagged mb15"):
#print(string)
#切片
commentId = str(string.get('id')).strip()[11:]
#print(commentId) #获取内容链接的后面id 9位数
print('\n')
#获取内容
print(floor, '.', string.find(attrs="content").get_text().strip())
floor += 1
#3.获取评论
commentPage = getContentOrComment(commentUrl%commentId)
soup = BeautifulSoup(commentPage, 'html.parser')
Cfloor = 1
for comment in soup.find_all(attrs="body"):
print("\n ", Cfloor, " 楼回复:", comment.get_text().strip())
Cfloor += 1
相关文章
- 【Python3网络爬虫开发实战】6.4-分析Ajax爬取今日头条街拍美图
- 【Python3网络爬虫开发实战】 3.2-使用requests
- 【Python3网络爬虫开发实战】1.6.2-Tornado的安装
- 【Python3网络爬虫开发实战】1.2.6-aiohttp的安装
- python3之编码详解
- 系统中同时有 python2和 python3,怎么让 ipython 选择不同的版本启动?
- python3:用pip安装第三方库时报错Command "python setup.py egg_info" failed with error code的解决
- Python3 文件操作
- 力扣——217. 存在重复元素(Java、C、python3实现)
- python2自动转换为python3
- 【Python3网络爬虫开发实战】1.2.3-ChromeDriver的安装
- 【Python3网络爬虫开发实战】1.5.2-PyMongo的安装
- 【Python3网络爬虫开发实战】 1.7-App爬取相关库的安装
- python3 定义向量运算
- Win10搭建python3环境
- 转 Python3 ssl模块不可用的问题
- python3 urllib和requests模块
- python3安装scrapy及使用方法(爬虫框架)