Getting started with python: web page data capture and python crawling

Source: Internet
Author: User

Getting started with python: web page data capture and python crawling

This is good. Just getting started.

1. Use feedparser:
Tip: use Universal Feed Parser to control RSS
Http://www.ibm.com/developerworks/cn/xml/x-tipufp.html
Please visit feedparser.org to learn more about the Universal Feed Parser, which also includes some download materials and documents.

Feedparser:
Http://code.google.com/p/feedparser/downloads/list

2. In addition, you need to add the UTF-8 bom header to the file and use python to write hexadecimal characters:
Http://linux.byexamples.com/archives/478/python-writing-binary-file/
Python writes hexadecimal characters
File. write ("\ x5F \ x9D \ x3E ")
File. close ()

3. to debug the file, change the open mode to w.

Python code
    1. Import urllib
    2. Import sys
    3. Import re
    4. From feedparser import _ getCharacterEncoding as enc
    5. Class TagParser:
    6. Def _ init _ (self, value ):
    7. Self. value = value
    8. Def get (self, start, end ):
    9. Regx = re. compile (R' <'+ start + R'. *?>. * </'+ End + R'> ')
    10. Return re. findall (regx, self. value)
    11. If _ name _ = "_ main __":
    12. Baseurl = "http://data.book.163.com/book/section/000BAfLU/000BAfLU"
    13. F = open ("test_01.txt", "w ")
    14. F. write ("\ xef \ xbb \ xbf ")
    15. # For ndx in range (0, 56 ):
    16. For ndx in range (0, 1 ):
    17. Url = baseurl + str (ndx) + ". html"
    18. Print "get content from" + url
    19. Src = urllib. urlopen (url)
    20. Text = src. read ()
    21. F1 = open ("tmp _" + str (ndx) + ". txt", "w ")
    22. F1.write (text)
    23. F1.close ()
    24. Encoding = enc (src. headers, text) [0]
    25. Tp = TagParser (text)
    26. Title = tp. get ('h1 class = "f26s tC" ', 'h1 ')
    27. Article = tp. get ('P class = "ti2em" ', 'P ')
    28. T = re. sub (R' </. +> ',' \ n', title [0])
    29. T = re. sub (R' <. +> ',' \ n', t)
    30. Data = t
    31. C = ""
    32. For p in article:
    33. Pt = re. sub (R' </p> ',' \ n', p)
    34. C + = pt
    35. C = re. sub (R' <. +> ',' \ n', c)
    36. Data + = c
    37. Data = data. decode (encoding)
    38. F. write (data. encode ('utf-8', 'ignore '))
    39. F. close ()

How can I extract the main content of a captured webpage using python?

Use beautiful soup
There are too many specific code. Check the link.
Reference: www.crummy.com/...h.html

Python captures webpage data

S = WAF webpage content
Try the following four: [try one by one] (by the way, are you from Minhang tech?
S = s. decode ("utf8 ")
S = s. decode ("gbk ")
S = s. encode ("utf8 ")
S = s. encode ("gbk ")

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.