Python crawler crawl page source code is shown on this page

Source: Internet
Author: User

When crawling Web content, the Python crawler needs to crawl the content together with the content format, and then display it in its own web page, defining a variable HTML for the Django framework, with a variable value of HTML code.
Print (HTML)<div id=1>  &nbsp;&nbsp;my <br> is   &nbsp; JAY <br>  </div>
, we now want to take the contents of the Div, display in our own web page, spaces and line breaks, etc. are crawled in the form of HTML code. The final desired data is &nbsp;&nbsp;my <br>&nbsp; Name <br>is &nbsp; JAY <br> (1) First soup.string is not possible, because there is more than one sub-label under the DIV
 from Import BeautifulSoup ' Html.parser ' )>>> soup.string
You can see that the return value is null (2) using Get_text () is also not possible, because Get_text () gets the string is escaped, we want the native HTML code
>>> soup.get_text ()'  \ \xa0\xa0my  \ \xa0 name \ n is \xa0 JAY \ n'
But Get_text () is useful on many other occasions, it can get the text content of all descendants ' tags under the tag and can specify parameters
>>> soup.get_text ('| ')  # delimiter for all tag text contents ' \ \xa0\xa0my | \ \xa0 name |\n is \xa0 JAY |\n '>>> soup.get_text ('| ', strip=true)   # Remove the whitespace before and after the text content 'my|name|is \xa0 JAY' 
or use the soup.stripped_strings generator to get text content manually (3) can be used. Contents
>>> Content_soup =soup.div.contents>>>content_soup['\ \xa0\xa0my', <br/>,'\ \xa0 Name', <br/>,'\ n is \xa0 JAY', <br/>,'\ n']>>> content_soup = [Str (i) forIinchContent_soup]#Change all values in the list to string types>>> Content_text ="'. Join (Content_soup)#merge the list into a string>>>Content_text'\ \xa0\xa0my <br/> \ \xa0 name <br/>\n is \xa0 JAY <br/>\n'>>>Print(Content_text) My<br/>name<br/> isJAY <br/>
At this point, you can put the variables directly into the Web page

Python crawler crawl page source code is shown on this page

Related Article

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.