python+selenium爬取微博热搜存入Mysql的实现方法
作者:也曾rgnxhw 时间:2024-01-24 18:07:53
最终的效果
废话不多少,直接上图
这里可以清楚的看到,数据库里包含了日期,内容,和网站link
下面我们来分析怎么实现
使用的库
import requests
from selenium.webdriver import Chrome, ChromeOptions
import time
from sqlalchemy import create_engine
import pandas as pd
目标分析
这是微博热搜的link:点我可以到目标网页
首先我们使用selenium对目标网页进行请求
然后我们使用xpath对网页元素进行定位,遍历获得所有数据
然后使用pandas生成一个Dataframe对像,直接存入数据库
一:得到数据
我们看到,使用xpath可以得到51条数据,这就是各热搜,从中我们可以拿到链接和标题内容
all = browser.find_elements_by_xpath('//*[@id="pl_top_realtimehot"]/table/tbody/tr/td[2]/a') #得到所有数据
context = [i.text for i in c] # 得到标题内容
links = [i.get_attribute('href') for i in c] # 得到link
然后我们再使用zip函数,将date,context,links合并
zip函数是将几个列表合成一个列表,并且按index对分列表的数据合并成一个元组,这个可以生产pandas对象。
dc = zip(dates, context, links)
pdf = pd.DataFrame(dc, columns=['date', 'hotsearch', 'link'])
其中date可以使用time模块获得
二:链接数据库
这个很容易
enging = create_engine("mysql+pymysql://root:123456@localhost:3306/webo?charset=utf8")
pdf.to_sql(name='infromation', con=enging, if_exists="append")
总代码
from selenium.webdriver import Chrome, ChromeOptions
import time
from sqlalchemy import create_engine
import pandas as pd
def get_data():
url = r"https://s.weibo.com/top/summary" # 微博的地址
option = ChromeOptions()
option.add_argument('--headless')
option.add_argument("--no-sandbox")
browser = Chrome(options=option)
browser.get(url)
all = browser.find_elements_by_xpath('//*[@id="pl_top_realtimehot"]/table/tbody/tr/td[2]/a')
context = [i.text for i in all]
links = [i.get_attribute('href') for i in all]
date = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime())
dates = []
for i in range(len(context)):
dates.append(date)
# print(len(dates),len(context),dates,context)
dc = zip(dates, context, links)
pdf = pd.DataFrame(dc, columns=['date', 'hotsearch', 'link'])
# pdf.to_sql(name=in, con=enging, if_exists="append")
return pdf
def w_mysql(pdf):
try:
enging = create_engine("mysql+pymysql://root:123456@localhost:3306/webo?charset=utf8")
pdf.to_sql(name='infromation', con=enging, if_exists="append")
except:
print('出错了')
if __name__ == '__main__':
xx = get_data()
w_mysql(xx)
来源:https://blog.csdn.net/wqeWED/article/details/113186957
标签:python,selenium,微博热搜,mysql
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python实习总结(yeild,async,azwait和协程)
2021-06-16 20:40:45
Python爬虫爬取百度搜索内容代码实例
2022-06-23 02:21:11
利用python进行接口测试及类型介绍
2021-02-11 21:27:01
![](https://img.aspxhome.com/file/2023/1/97671_0s.png)
jQuery实现简单复制json对象和json对象集合操作示例
2024-04-16 09:29:17
![](https://img.aspxhome.com/file/2023/7/136627_0s.png)
没编程基础可以学python吗
2023-11-27 23:12:49
Go语言实现的树形结构数据比较算法实例
2023-08-06 18:18:39
跟老齐学Python之编写类之四再论继承
2022-01-08 10:18:12
![](https://img.aspxhome.com/file/2023/7/118497_0s.png)
Python字典和集合讲解
2021-03-05 22:31:14
![](https://img.aspxhome.com/file/2023/2/65552_0s.png)
调整Jupyter notebook的启动目录操作
2022-04-02 19:40:58
详解python之配置日志的几种方式
2022-11-02 14:32:30
python实现清屏的方法
2021-11-02 04:10:32
Python办公自动化PPT批量转换操作
2023-11-07 16:54:20
![](https://img.aspxhome.com/file/2023/7/90387_0s.jpg)
Python命令行解析器argparse详解
2023-06-14 01:08:14
python中的Numpy二维数组遍历与二维数组切片后遍历效率比较
2022-11-23 04:47:50
python在地图上画比例的实例详解
2023-06-22 20:54:00
![](https://img.aspxhome.com/file/2023/1/99011_0s.png)
Django实现基于类的分页功能
2022-04-06 21:49:03
![](https://img.aspxhome.com/file/2023/5/118775_0s.jpg)
离线状态下在jupyter notebook中使用plotly实例
2022-04-19 18:05:59
![](https://img.aspxhome.com/file/2023/6/65466_0s.jpg)
mysql数据库优化需要遵守的原则
2024-01-15 13:54:29
Python自动安装第三方库的小技巧(pip使用详解)
2021-07-15 19:32:48
![](https://img.aspxhome.com/file/2023/0/131730_0s.jpg)
使用python检查值是否已经存在于字典列表中
2023-10-25 03:08:21
![](https://img.aspxhome.com/file/2023/1/84951_0s.png)