Giter Club home page Giter Club logo

spider's Introduction

Spider

新浪微博爬虫(Sina weibo spider),百度搜索结果 爬虫

(有疑问请发 Issue,邮件可能无法及时回复)

使用方法:

环境:

python 版本 >=python3.5

mysql 版本 >= 5.5.3

pip 版本应与 python 一样

推荐使用虚拟环境:

cd weibo
chmod u+x env.sh
source env.sh

全局安装(不使用虚拟环境):

pip3 install -r requirements.txt

代码运行流程:

有两个爬虫程序 sina_spider.pyweibo_spider.py ,区别在于 weibo_spider.py 使用了 selenium ,可以自动登录并且访问网址不容易出现403,但是该程序要慢一些。所以建议首选 sina_spider.py,如果出现403,再改用weibo_spider.py。两个程序爬取的结果是一样的。

  • 采用 sina_spider.py 爬取的运行顺序:

0.若是使用的虚拟环境,每次运行前使用 source env.sh 命令进入虚拟环境

1.首先根据自己的情况修改 conf.yaml ,该文件内有详细说明,关于 uid 和 cookie的获取请见这篇文章

2.然后运行 Create_all.py 创建MySQL表

3.接着运行 sina_spider.py 爬取微博用户资料和动态并保存在数据库中

4.然后分别运行 Data_analysis.pyLDA_Analysis.py 对数据进行处理

5.得到数据处理结果(默认为 weibo.jpg,weibo_wordfrq.html,weibo_dynamic.html,lda.html)

  • 采用 weibo_spider.py 爬取的运行顺序:

0.安装浏览器驱动,请先阅读我的这篇文章

1.若是使用的虚拟环境,每次运行前使用 source env.sh 命令进入虚拟环境

2.首先根据自己的情况修改 conf.yaml ,该配置文件内有详细说明,不需要cookies,但是需要填写用户名和密码,修改目标uid,然后可以根据需要修改 MySQL 配置

3.然后运行 Create_all.py 创建MySQL表

4.接着运行 weibo_spider.py 爬取微博用户资料和动态并保存在数据库中

5.然后分别运行 Data_analysis.pyLDA_Analysis.py 对数据进行处理

6.得到数据处理结果(默认为 weibo.jpg,weibo_wordfrq.html,weibo_dynamic.html,lda.html)

具体说明请见源码。

本项目系列文章

关于本项目代码以及处理的具体思路可以阅读作者的这三篇文章:

爬虫实战(一):爬取微博用户信息

爬虫实战(二):Selenium 模拟登录并爬取信息

爬虫实战(三):微博用户信息分析

更为详细的说明请见源码及注释。

微博用户信息分析

可以爬取微博用户个人资料以及动态信息。

数据分析:

  • 生成词云
  • 统计词频
  • 统计活跃时间
  • 使用 LDA 构建了微博主题模型
  • 更多功能...

UI:

  • 生成良好的UI数据分析与展示界面

更多功能还在开发中,程序也在不断重构中.... 代码行数++

代码结构

├── baidu_result
│   └── baidu_result.py	#根据关键词爬取百度搜索结果
├── LICENSE
├── README.md
└── weibo
    ├── conf.yaml			#配置文件
    ├── Connect_mysql.py	#连接数据库
    ├── Create_all.py		#创建MySQL表
    ├── data_analysis		
    │   ├── data			
    │   │   └── stop_words.txt	#中文停用词
    │   ├── Data_analysis.py	#根据微博用户动态进行词云和词频分析
    │   ├── HYQiHei-25J.ttf		#用于生成中文词云的字体
    │   └── LDA_Analysis.py		#使用LDA进行微博动态主题建模与分析
    ├── Delete_users.py			#删除不在conf.yaml配置文件中的微博用户及其动态
    ├── env.sh					#该脚本可以使程序运行在虚拟环境中
    |── get_cookies.py			#模拟登录微博并获取cookies
    ├── __init__.py
    ├── requirements.txt		#项目依赖文件
    ├── sina_spider.py			#爬取微博用户资料和动态并保存在数据库中(速度快)
    └── weibo_spider.py			#爬取微博用户资料和动态并保存在数据库中(效果好)

数据分析示例

词云:

weibo.jpg

词频分析:

weibo_wordfrq.gif

微博动态时间统计:

weibo_dynamic.gif

微博主题分析(LDA):

lda.gif

协议

本项目遵从MIT协议

spider's People

Contributors

starfalll avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

spider's Issues

AttributeError: 'str' object has no attribute 'get'

大佬,在运行Data-analysis的时候会报错
Traceback (most recent call last):
File "E:/python/python项目/Spider-master/weibo/data_analysis/Data_analysis.py", line 150, in
main(uid)#指定需要分析的用户的uid(必须先存在conf.yaml里面,并且运行了一次sina_spider程序),默认为conf.yaml中的第一条uid
File "E:/python/python项目/Spider-master/weibo/data_analysis/Data_analysis.py", line 135, in main
plot_create_time(time_lists)
File "E:/python/python项目/Spider-master/weibo/data_analysis/Data_analysis.py", line 128, in plot_create_time
chart = Bar('用户微博动态发布时间')
File "E:\python\lib\site-packages\pyecharts\charts\chart.py", line 163, in init
super().init(init_opts=init_opts)
File "E:\python\lib\site-packages\pyecharts\charts\chart.py", line 14, in init
super().init(init_opts=init_opts)
File "E:\python\lib\site-packages\pyecharts\charts\base.py", line 29, in init
self.width = _opts.get("width", "900px")
AttributeError: 'str' object has no attribute 'get'
在百度上找的原因好像都不对,请问怎么解决啊。

运行weibo_spider.py报错

会报一个这样的错:FileNotFoundError: [Errno 2] No such file or directory: 'cookies.pkl'
报错具体如下:

(WeiboSpider)jiang@ubuntu:~/Spider/weibo$ sudo python3.5 weibo_spider.py Traceback (most recent call last): File "weibo_spider.py", line 205, in <module> main() File "weibo_spider.py", line 194, in main cookies = pickle.load(open('cookies.pkl', 'rb')) FileNotFoundError: [Errno 2] No such file or directory: 'cookies.pkl'

请赐教

您好
我在实现的过程中遇到了问题,想请您指点一下,信息,方便的话请加我的微信:yandayileng
谢谢。

大佬,百度请求页面只能获取第一页,后面的页面需要验证

我的搜索如下:
data = {'wd': "intitle:疫情 intitle:武汉 site:www.caixin.com", 'tn': 'baiduhome_pg', 'ie': 'utf-8', 'bsst': 1, 'pn': str(i - 1) + '0', }
第一页返回非常正常,获取到的链接,比如下一页的链接,再用page=requests.get(next_page_url, headers=headers)
就无法获取有效信息了,soup的值打印如下:
应该是一个验证码的问题。贼难受,如果大佬之前遇到过,恳请大佬指导!

`soup: <!DOCTYPE html>

<html lang="zh-CN">
<head>
<meta charset="utf-8"/>
<title>ç¾åº¦å®å
¨éªè¯</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<meta content="yes" name="apple-mobile-web-app-capable"/>
<meta content="black" name="apple-mobile-web-app-status-bar-style"/>
<meta content="width=device-width, user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0" name="viewport"/>
<meta content="telephone=no, email=no" name="format-detection"/>
<link href="https://www.baidu.com/favicon.ico" rel="shortcut icon" type="image/x-icon"/>
<link href="https://www.baidu.com/img/baidu.svg" mask="" rel="icon" sizes="any"/>
<meta content="IE=Edge" http-equiv="X-UA-Compatible"/>
<meta content="upgrade-insecure-requests" http-equiv="Content-Security-Policy"/>
<link href="https://wappass.bdimg.com/static/touch/css/api/mkdjump_8befa48.css" rel="stylesheet">
</link></head>
<body>
<div class="timeout hide">
<div class="timeout-img"></div>
<div class="timeout-title">ç½ç»ä¸ç»å¼è¯·ç¨åéè¯</div>
<button class="timeout-button" type="button">è¿å¦é¡µ</button>
</div>
<div class="timeout-feedback hide">
<div class="timeout-feedback-icon"></div>
<p class="timeout-feedback-title">é®é¢é¦</p>
</div>
<script src="https://wappass.baidu.com/static/machine/js/api/mkd.js"></script>
<script src="https://wappass.bdimg.com/static/touch/js/mkdjump_2e06726.js"></script>
</body>
</html>`

Data_analysis.py运行问题

大佬好,请问一下我在将用户的数据爬取下来保存到Mysql以后,怎么运行Data_analysis.py文件会出现
Traceback (most recent call last):
File "E:/python/python项目/Spider-master/weibo/data_analysis/Data_analysis.py", line 150, in
main(uid)#指定需要分析的用户的uid(必须先存在conf.yaml里面,并且运行了一次sina_spider程序),默认为conf.yaml中的第一条uid
File "E:/python/python项目/Spider-master/weibo/data_analysis/Data_analysis.py", line 135, in main
plot_create_time(time_lists)
File "E:/python/python项目/Spider-master/weibo/data_analysis/Data_analysis.py", line 117, in plot_create_time
long_lists.append(res[0])
IndexError: list index out of range
尝试打印了一下res,发现确实是空的,请问一下怎么解决

cookies.pkl

这个文件是干什么的,是生成的还是需要自己去创建的

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.