ok 在学习完了js后,重新拾取py爬虫系列了,不太喜欢每天学很多种系列,就一天python,一天jquery开始更把,希望能快点学习完。目前计划 jquery-vue-小程序 这是前端的 python -爬虫- 数据分析或者机器学习慢慢来更。requests 库我就不在多介绍了 我这个爬虫算重新复习就大概以前写过相似的案例还在写了一次。找了一个感觉比较好的老师。
单页图片:
import requests
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36'
}
url = 'http://p1.music.126.net/5q799ZDsUfjRZdbSE7pt3A==/109951167201406735.jpg?imageView&quality=89'
res = requests.get(url,headers=headers)
with open('nesate.jpg','wb') as f:
f.write(res.content)
图片操作很简单,找到图片后面点击复制url 就行了自己保存一下就可以了。
单首歌曲:
url = 'https://m801.music.126.net/20220327210838/3699d2e6ff074dbfac6b848915f6704f/jdyyaac/0e0f/065f/5153/9e5c47486015ab1f9242f9230507cef1.m4a'
res2 = requests.get(url,headers=headers)
with open('nesate.mp3','wb') as f:
f.write(res2.content)
一样的操作 ,不过抓包要选择MP4A 的吧好多音乐应该都是这个 视频也是一样的操作我就不放出来了 视频后面的文件media就行了。
分页操作:
首先根据不同页数的 pn值的规律分析一下url 就可以进行爬取了 观察 kw参数就是搜查内容就可以了
直接上代码:
#https://tieba.baidu.com/f?kw=csgo&ie=utf-8&pn=0 第一页
# https://tieba.baidu.com/f?kw=csgo&ie=utf-8&pn=50 第二页
# https://tieba.baidu.com/f?kw=csgo&ie=utf-8&pn=100 第三页
# https://tieba.baidu.com/f?kw=csgo&ie=utf-8&pn=150 第四页
url = 'https://tieba.baidu.com/f?'
kw = input('查找内容')
page = int(input('查找页数'))
for i in range(page):
params = {
'kw':kw,
'pn':i*50
}
res = requests.get(url,headers=headers,params=params)
with open(f'{kw}{i+1}.html','wb') as f:
f.write(res.content)
用对象进行封装
类还是蛮重要的,好像有点忘了要好好补一下
class Tieba:
def __init__(self):
self.url ='https://tieba.baidu.com/f?'
self.headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36'
}
def send(self,params):
res = requests.get(self.url,headers=self.headers,params=params)
return res.text
def save(self,page,con):
with open(f'{page}.html','w',encoding='utf-8') as f:
f.write(con)
def run(self):
word = input('内容')
page = int(input('页数'))
for page in range(page):
params= {
'kw':word,
'pn':page*50
}
data = self.send(params)
self.save(page,data)
te = Tieba()
te.run()
爬取周杰伦2页
最后一个就是翻译了:
应为是post请求 要带参数 分析一下就行不是很难
# post 请求案例 -- 金山翻译
# url = 'https://ifanyi.iciba.com/index.php?c=trans'
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36'
}
# 参数字典
kw= input('翻译内容')
post_data= {
'from': 'en',
'to': 'zh',
'q': kw,
}
这个只是英文转中文,可以改变from to 参数实现中换英文。
大概就是这么多了下面总结环境先附上今天的学习进度21/?? 不知道有多少先看了21