爬虫(二)使用urllib爬取百度贴吧的数据

下一期我就不用urllib来抓取数据了,因为urllib现在已经很少人用,大部分人用得是requests,requests也是基于底层urllib的一个模块。

首先我先来讲一下关于如何使用动态的UA!

动态UA就是指在自己创建的一个列表里随机选择一个UA当做请求浏览器的一个请求头.

我们先自定义一个列表User_Agents,然后将要添加的UA传进去.
UA大全

python 复制代码
User_Agents = [
        'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
        'Opera/9.80 (Windows NT 6.1; U; zh-cn) Presto/2.9.168 Version/11.50',
        'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.3)']
        

传入进去之后我们使用random模块中的choice去随机选择UA

python 复制代码
from random import  choice
headers = {'User-Agent': choice(User_Agents)
               }

至此一个动态UA就完成了!

接下来给大家献上爬取百度贴吧的代码

思路:我们知道要爬取有页数的url就要找出其中的规律

我们根据百度贴吧每页翻页的url可以得出基础的url是base_html='https://tieba.baidu.com/f?ie=utf-8\&{}

python 复制代码
from urllib.request import urlopen,Request
from random import  choice
from urllib.parse import quote,urlencode
 #定义一个得到url数据的函数
def get_html(url):
    User_Agents = [
        'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
        'Opera/9.80 (Windows NT 6.1; U; zh-cn) Presto/2.9.168 Version/11.50',
        'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.3)']

    headers = {'User-Agent': choice(User_Agents)

               }
	
	#伪装浏览器
    response = Request(url=url, headers=headers)
    #获取响应
    html = urlopen(response)
    #把读的到数据返回
    return html.read()

#定义一个保存数据文件的函数
def save_html(file_name,html_bytes):
    with open(file_name,'wb') as rfile:
        rfile.write(html_bytes)
        rfile.close()
#定义一个主函数
def main():
    concent = input('请输入要下载的内容:')
    num=input('请输入要下载的页数:')
    base_html='https://tieba.baidu.com/f?ie=utf-8&{}'
    for pn in range(int(num)):
        arg={
            'pn':pn*50,
            'kw':concent
        }
        file_name='第'+str(pn+1)+'页'
        #将arg转码
        arg = urlencode(arg)
        print(f'正在下载{concent}的'+file_name)
		#base_html='https://tieba.baidu.com/f?ie=utf-8&{pn*50}
        html=base_html.format(arg)
        #得到网页里的数据
        html_bytes=get_html(html)
        save_html(file_name,html_bytes)

#让程序跑起来
if __name__ == '__main__':
    main()
    print('下载完成')

这是爬取内容个每页的源代码,要获取相应的图片和内容还需要去解析提取。

看完的点个赞吧,喜欢的可以点点关注哦!

相关推荐
深蓝电商API3 小时前
0 基础入门爬虫:Python+requests 环境搭建保姆级教程
开发语言·爬虫·python
B站计算机毕业设计之家6 小时前
基于Python+Django+双协同过滤豆瓣电影推荐系统 协同过滤推荐算法 爬虫 大数据毕业设计(源码+文档)✅
大数据·爬虫·python·机器学习·数据分析·django·推荐算法
孤狼warrior11 小时前
目前最新同花顺金融股市数据爬取 JS逆向+node.js补浏览器环境
javascript·爬虫·python·金融·node.js
xiaoxiongip6661 天前
假设两个设备在不同网段,网关怎么设置才能通呢
网络·爬虫·python·https·智能路由器
tryCbest1 天前
Python基础之爬虫技术(一)
开发语言·爬虫·python
疏狂难除1 天前
某个网址的爬虫——mitmproxy的简单使用
爬虫·mitmproxy
想要打 Acm 的小周同学呀1 天前
爬虫相关的面试问题
爬虫·selenium·职场和发展
道可云1 天前
制造强国新图景实践——解析《实施意见》对制造业的影响
百度·制造
QMY5205201 天前
爬虫技术抓取网站数据的方法
运维·爬虫·自动化
Ma0407131 天前
【论文阅读16】-LLM-TSFD:一种基于大型语言模型的工业时间序列人机回路故障诊断方法
百度