Python Learning notes Crawler 1

Source: Internet
Author: User

Operation Steps of the crawler:

Reptile three-Step walk
    • Reptile First step: Use requests to get data:
      1. Import Requests
      2. Use Requests.get to get the Web source
    • Import REQUESTSR = Requests.get (' https://book.douban.com/subject/1084336/comments/'). Text

        

        

    • Crawler Second step: Use BEAUTIFULSOUP4 to parse the data:
      1. Import BS4
      2. Parsing Web page data
      3. Finding data
      4.for Cycle Printing
From BS4 Import Beautifulsoupsoup = BeautifulSoup (R, ' lxml ') pattern = soup.find_all (' P ', ' comment-content ') for item in Pattern:print (item.string)

  

    • Crawler Step Three: Use Pandas to save data:
      1. Import Pandas
      2. Create a new List object
      3. Write using To_csv
Import pandascomments = []for item in Pattern:comments.append (item.string) df = Pandas. DataFrame (comments) df.to_csv (' Comments.csv ')

The complete reptile
Import REQUESTSR = Requests.get (' https://book.douban.com/subject/1084336/comments/'). Text from BS4 Import Beautifulsoupsoup = BeautifulSoup (R, ' lxml ') pattern = soup.find_all (' P ', ' comment-content ') for item in Pattern:print ( item.string) Import pandascomments = []for item in Pattern:comments.append (item.string) df = Pandas. DataFrame (comments) df.to_csv (' Comments.csv ')

Code Run Result:

  

Python Learning notes Crawler 1

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.