博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python 百度热搜 全页面爬取
阅读量:2094 次
发布时间:2019-04-29

本文共 2253 字,大约阅读时间需要 7 分钟。

import requestsimport xlwtfrom bs4 import BeautifulSoupdef getCid():    hd = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36 SLBrowser/7.0.0.4071 SLBChan/21"} url = "https://top.baidu.com/board" page = requests.get(url,headers=hd).content.decode("utf-8") soup = BeautifulSoup(page, 'html.parser') dIv = soup.find_all('a', attrs={
'class':"tab-item_17MbK"}) code = [] type_L = [] for i in dIv: src = i['href'] code.append(src.strip('.')) type_L.append(i.text) return code,type_Ldef getItem(page): hd = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36 SLBrowser/7.0.0.4071 SLBChan/21"} url = "https://top.baidu.com%s" url = url%page page = requests.get(url,headers=hd).content.decode("utf-8") soup = BeautifulSoup(page, 'html.parser') code = [] tBody = soup.find('div',attrs={
'style':"margin-bottom:20px"}) name_M = tBody.find_all('a',attrs={
'class':"title_dIF3B"}) text_M = tBody.select("div[class^='hot-desc_1m_jR large_nSuFU']") top_M = tBody.find_all('a', attrs={
'class': "img-wrapper_29V76"}) score_M = tBody.find_all('div', attrs={
'class': "hot-index_1Bl1a"}) for i in range(0,len(name_M)): # code.append(dict(排名=top_M[i].div.text,标题=name_M[i].text,分数=score_M[i].text)) code.append([top_M[i].div.text,name_M[i].text,score_M[i].text]) return codedef saveExcel(result,type_L): wb = xlwt.Workbook() # 创建工作表 for i in range(0,len(type_L)): sheet = wb.add_sheet(type_L[i]) list_h = ['排名','标题','分数'] for row,str in enumerate(list_h): sheet.write(0, row, str) for m in range(0,len(result[i])): for n,inV in enumerate(list_h): sheet.write(m+1,n,result[i][m][inV]) wb.save('热搜全爬.xlsx')from openpyxl import Workbookdef OaveExcel(data): workbook = Workbook() sheet = workbook.create_sheet() for x in data: sheet.append(x) workbook.save('热搜全爬.xlsx')page,type_L=getCid()all_S = []del page[0]del type_L[0]for i in range(0,len(page)): all_S.append(getItem(page[i]))saveExcel(all_S,type_L)

转载地址:http://jbuhf.baihongyu.com/

你可能感兴趣的文章
数据类型 java转换
查看>>
"NetworkError: 400 Bad Request - http://172.16.47.117:8088/rhip/**/####t/approval?date=976
查看>>
mybatis 根据 数据库表 自动生成 实体
查看>>
C结构体、C++结构体、C++类的区别
查看>>
进程和线程的概念、区别和联系
查看>>
CMake 入门实战
查看>>
绑定CPU逻辑核心的利器——taskset
查看>>
Linux下perf性能测试火焰图只显示函数地址不显示函数名的问题
查看>>
c结构体、c++结构体和c++类的区别以及错误纠正
查看>>
Linux下查看根目录各文件内存占用情况
查看>>
A星算法详解(个人认为最详细,最通俗易懂的一个版本)
查看>>
利用栈实现DFS
查看>>
(PAT 1019) General Palindromic Number (进制转换)
查看>>
(PAT 1073) Scientific Notation (字符串模拟题)
查看>>
(PAT 1080) Graduate Admission (排序)
查看>>
Play on Words UVA - 10129 (欧拉路径)
查看>>
mininet+floodlight搭建sdn环境并创建简答topo
查看>>
【linux】nohup和&的作用
查看>>
Set、WeakSet、Map以及WeakMap结构基本知识点
查看>>
【NLP学习笔记】(一)Gensim基本使用方法
查看>>