Code Monkey home page Code Monkey logo

weibo-crawler's Introduction

Sina Weibo Crawler

image image image

English Version | 中文版

This is a Python implementation of crawling Weibo data (e.g., text, images, live photos, and videos) of a Sina Weibo user from the Weibo Mobile Client. It simulates user login by a session (username and password).

Many thanks to Python Chinese Community for providing the source code SourceCode_weibocrawler.py.

Functions

  • Crawling short text in original and retweeted Weibo posts.

  • Crawling large (preferred) or small JPG/GIF images in original and retweeted Weibo posts.

  • Crawling live photos (as JPG images, MOV videos, and/or GIF images) in original and retweeted Weibo posts.

  • Crawling HD (preferred) or SD videos in original and retweeted Weibo posts.

Environment

This code has been tested on Ubuntu 16.04 operating system. For Windows and macOS, the format of folder path should be changed accordingly.

Dependencies

  • requests 2.21.0
  • lxml 4.2.5
  • cv2 4.1.0
  • imageio 2.4.1
  • PIL 5.3.0

Usage

User Settings

  1. Set S_DATA and S_HEADER of the session for simulating Sina Weibo user login (see comments for details).

  2. Set USER_URL of the target Weibo user (see comments for details).

  3. Set the amount of pages (PAGE_AMOUNT) for crawling (see comments for details).

  4. Set the path (PATH_FOLDER) and the TXT file (PATH_FILE_TXT) for saving Weibo data.

  5. Set the type of Weibo data (IF_IMAGE, IF_PHOTO, and IF_VIDEO as 1) for crawling.

  6. Set IF_LIVE2GIF = True if live photos (videos) need to be converted to GIF images.

  7. Set TIME_DELAY of the crawler to avoid ConnectionError 104: ('Connection aborted.').

  8. If ConnectionError 104: ('Connection aborted.') occurs:

    1. Set IF_RECONNECT = True for running the crawler in reconnection mode.

    2. Set TAG_STARTCARD as the serial number of the stopping Weibo post (according to log information).

    3. Re-run run_WeiboCrawler.py to continue to crawl Weibo data.

    4. Set IF_RECONNECT = False if run the crawler in normal mode!

Run

  1. Run run_WeiboCrawler.py to crawl Weibo data of the target Sina Weibo user.

  2. See Log_run_WeiboCrawler.txt for log information of running the code.

Results

  1. The Weibo data will be saved in the pre-specified folder (e.g., Demo_WeiboData/).

  2. The text of Weibo posts will be saved in the TXT file (e.g., Demo_WeiboData/Demo_WeiboPost_Records.txt).

  3. The images, live photos, and videos will be saved in sub-folders (e.g., Demo_WeiboData/1/, Demo_WeiboData/1_livephoto/, and Demo_WeiboData/1_video/).


Please report an issue if you have any question about this repository, I will respond ASAP.

Please star this repository if you found its content useful. Thank you very much. ^_^

weibo-crawler's People

Contributors

hezhang1994 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

weibo-crawler's Issues

KeyError: 'pic_video'

Error message:
Traceback (most recent call last):
File "H:/prbkp/weibo-crawler/run_WeiboCrawler.py", line 245, in
photo_str = card['mblog']['pic_video']
KeyError: 'pic_video'

I do not know why it appeared. I just use:
if card_info:
if 'pic_video' in card_info:
try:
photo_str = card['mblog']['pic_video']
photo_list = re.split('[,]', photo_str)
for photo in photo_list:
# E.g., 'photo' = '0:000voDMsjx07t57qM583010f0100alDF0k01'.
photo_code = re.split('[:]', photo)[1]
photo_url = photo_url_start + photo_code + photo_url_end
photo_urls.append(photo_url)
except:
pass

to skip the part of code to prevent sudden interruption.

Hope it can help for further improvement.

when time_list == 'N/A\r', it would break

Sometimes, the value of time_list could be ['N/A\r'] (def time_str2float). It may mean the video missed. Then, it would break.
My temporary solution is that set 'time_num = 0', if the 'N/A\r' appears. However, I do not know if it would cause unknown flaws.

Unknown Bug

It happens at the following code:
if card['mblog']['isLongText'] == 'False': ... else: ...

In details, when card['mblog']['isLongText'] = False, the code still excutes the else content.

Limitation

  1. If the cookie is expired, the crawling process will stop.

    • How to continue to crawl data from the stopped post?
    • How to automatic crawl data?
  2. How to crawl videos?

  3. How to crawl GIFs?

抓取到live photo会报错

换了不同人的账号,一到live photo就会报错
FileNotFoundError: [WinError 2] 系统找不到指定的文件
谢谢

Limitations

  1. If the cookie is expired, the crawling process will stop.

    • How to continue to crawl data from the stopped post?
    • How to automatic crawl data?
  2. How to crawl video?

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.