Python captures the latest blog content and generates Rss

Source: Internet
Author: User

Python captures the latest blog content and generates Rss

This article will share with you how to use python to capture the latest blog content and generate Rss code. It mainly uses the PyRSS2Gen method, which is very simple and practical. If you need it, you can refer to it.

The rss of osc is not output in full text, so it is not happy that python captures the latest osc blog to generate Rss

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

#-*-Coding: UTF-8 -*-

 

 

From bs4 import BeautifulSoup

Import urllib2

 

Import datetime

Import time

Import PyRSS2Gen

From email. Utils import formatdate

Import re

Import sys

Import OS

Reload (sys)

Sys. setdefaultencoding ('utf-8 ')

 

Class RssSpider ():

Def _ init _ (self ):

Self. myrss = PyRSS2Gen. RSS2 (title = 'ossina ',

Link = 'HTTP: // my.oschina.net ',

Description = str (datetime. date. today ()),

PubDate = datetime. datetime. now (),

LastBuildDate = datetime. datetime. now (),

Items = []

)

Self. xmlpath = R'/var/www/myrss/oschina. xml'

 

Self. baseurl = "http://www.oschina.net/blog"

# If OS. path. isfile (self. xmlpath ):

# OS. remove (self. xmlpath)

Def useragent (self, url ):

I _headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64)

AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36 ",

"Referer": 'http: // baidu.com /'}

Req = urllib2.Request (url, headers = I _headers)

Html = urllib2.urlopen (req). read ()

Return html

Def enterpage (self, url ):

Pattern = re. compile (r 'd {4} Sd {2} Sd {2} sd {2} Sd {2} Sd {2 }')

Rsp = self. useragent (url)

Soup = BeautifulSoup (rsp)

Timespan = soup. find ('div ', {'class': 'blogstat '})

Timespan = str (timespan). strip (). replace ('n', ''). decode ('utf-8 ')

Match = re. search (r 'd {4} Sd {2} Sd {2} sd {2} Sd {2} ', timespan)

Timestr = str (datetime. date. today ())

If match:

Timestr = match. group ()

# Print timestr

Ititle = soup. title. string

Div = soup. find ('div ', {'class': 'blogcontent '})

Rss = PyRSS2Gen. RSSItem (

Title = ititle,

Link = url,

Description = str (div ),

PubDate = timestr

)

 

Return rss

Def getcontent (self ):

Rsp = self. useragent (self. baseurl)

Soup = BeautifulSoup (rsp)

Ul = soup. find ('div ', {'id': 'centblogs '})

For li in ul. findAll ('lil '):

Div = li. find ('div ')

If div is not None:

Alink = div. find ('A ')

If alink is not None:

Link = alink. get ('href ')

Print link

Html = self. enterpage (link)

Self. myrss. items. append (html)

Def SaveRssFile (self, filename ):

Finallxml = self. myrss. to_xml (encoding = 'utf-8 ')

File = open (self. xmlpath, 'w ')

File. writelines (finallxml)

File. close ()

 

 

 

If _ name __= = '_ main __':

RssSpider = RssSpider ()

RssSpider. getcontent ()

RssSpider. SaveRssFile ('oschina. xml ')

The above is all the content of this article. I hope you will like it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.