python爬虫实战(一)→requests

现在说起爬虫,大部分人的第一反应就是python爬虫。
那么今天就来说说python爬虫

Requests是使用Apache2 licensed 许可证的HTTP库,用python编写较简洁。
Request支持HTTP连接保持和连接池,支持使用cookie保持会话,支持文件上传,支持自动响应内容的编码,支持国际化的URL和POST数据自动编码。

在python内置模块的基础上进行了高度的封装,从而使得python进行网络请求时,变得人性化,使用Requests可以轻而易举的完成浏览器可有的任何操作。requests会自动实现持久连接keep-alive,较为友好。

① 导入模块

import requests

② 发送请求

url = www.baidu.com
response = requests.get(url=url)

③设置请求头
我们在使用python爬虫过程中,大多数需要伪装成浏览器发送请求

	url= 'xxxxxxxxxxxxxxxxxxxxxxx'
	header = {
		'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36',
		'Cookie': 'xxxxxxxxxxxxxxxxxxxx'
	}

④获取响应内容

response = requests.get(url=url,headers=header)
这里,我们可以使用 .content 或者 .text 来获取响应内容

⑤编码解码

我们可以使用 encoding   decode 等方式
也可以使用 json的方式
import json
json.loads(response)

最后我们只要在返回内容里面找到自己想要的数据即可

最够附上实战内容:

    import requests
	import json
	import time

class spider_():
    def __init__(self):
        #影视
        # self.url = 'https://sv.baidu.com/videoui/api/videorec?tab=yingshi&act=pcFeed&pd=pc&num=20&shuaxin_id=1589354614665'
        #音乐
        # self.url = 'https://sv.baidu.com/videoui/api/videorec?tab=yinyue&act=pcFeed&pd=pc&num=20&shuaxin_id=1589354123512'
        #搞笑
        self.url = 'https://sv.baidu.com/videoui/api/videorec?tab=gaoxiao&act=pcFeed&pd=pc&num=20&shuaxin_id=1589354553450'
        #娱乐
        # self.url = 'https://sv.baidu.com/videoui/api/videorec?tab=yule&act=pcFeed&pd=pc&num=20&shuaxin_id=1589354718578'
        #动漫
        # self.url = 'https://sv.baidu.com/videoui/api/videorec?tab=dongman&act=pcFeed&pd=pc&num=20&shuaxin_id=1589354765572'
        #综艺
        # self.url = ' https://sv.baidu.com/videoui/api/videorec?tab=zongyi&act=pcFeed&pd=pc&num=20&shuaxin_id=1589354801895'
        #军事
        # self.url = ' https://sv.baidu.com/videoui/api/videorec?tab=junshi&act=pcFeed&pd=pc&num=20&shuaxin_id=1589354839248'
        #科技
        # self.url = ' https://sv.baidu.com/videoui/api/videorec?tab=keji&act=pcFeed&pd=pc&num=20&shuaxin_id=1589354870549'
        self.header = {
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
        'tab':'yingshi',
        'act':'pcFeed',
        'pd':'pc',
        'shuaxin_id':'1589334676944',
        'num':'20',
        'cookie':'PSTM=1564311462; BIDUPSID=9478F7985C38E78B92F7AF6BF89EB9D4; MCITY=-%3A; BAIDUID=B22141A6F325C4F908B57B4BFBF91AA1:FG=1; BDUSS=GxoZW9yNkNHNC14V3pPeU5SMjUyTkQxWE5qMXU4b0FCZFduTEJhSXB2Z2xDcmxlRVFBQUFBJCQAAAAAAAAAAAEAAADj43o4YWlyZWFkMTMxNAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACV9kV4lfZFeN; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; yjs_js_security_passport=71c6112c4a650adf0cbf830b896a17f788cb3776_1589189848_js; cflag=13%3A3; H_PS_PSSID=31358_1420_31325_21126_31253_31463_31228_30823_31163; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; delPer=0; PSINO=2; Hm_lvt_4aadd610dfd2f5972f1efee2653a2bc5=1589246280; COMMON_LID=164cc820a7c5e83078effb0043f75ebc; PC_TAB_LOG=haokan_website_page; Hm_lpvt_4aadd610dfd2f5972f1efee2653a2bc5=1589254822; reptileData=%7B%22data%22%3A%2262e08890e27c44ea563adb1238af2cbb446a5e37376e760d65628066c4605508cbaf1d69bda4c7c207ef6350b3e4ab57ff80e934d947be2a867535ce7e70651e15fde67845472caeadbffee6b481e5e4165a36903f534ce8dfc4392dc2bbf72ab345d1cec944c40ecdf8425250efe5b080014a3d26db3fb6e842594b8de5dbcc882e620ae659aaa3585fecb3d2834979%22%2C%22key_id%22%3A%2230%22%2C%22sign%22%3A%220a4cd7af%22%7D'
    }

def down_videos(self):
    #防止被发现 每爬取20条 等待3秒
    time.sleep(3)
    self.response = requests.get(url=self.url, headers=self.header).content
    self.response = json.loads( self.response)
    self.response_ =  self.response['data']['response']['videos']
    print(self.response_)
    for item in range(1):
        for i in self.response_:
            filename = i['title'] + '.mp4'
            with open(filename, "wb") as f:
                f.write(requests.get(i['play_url']).content)
                print(filename, '下载成功')

if name == ‘main’:
spider_().down_videos()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值