Using BeautifulSoup to crawl the content of Sina Web News

Source: Internet
Author: User

For the first time, Python is really powerful, and the routed line of code crawls the content and stores it as a txt text

Directly on the code

#coding = ' Utf-8 'ImportRequests fromBs4ImportBeautifulSoupImportsysreload (SYS) sys.setdefaultencoding ("Utf-8")#Crawling Web pagesURL ="http://news.sina.com.cn/china/"Res=requests.get (URL) res.encoding='Utf-8'#put it in the soup and analyze the Web content .Soup = BeautifulSoup (Res.text,"Html.parser") Elements= Soup.select ('. News-item')    #grab what you need and put it in a file#crawl content with time, content text, and links to contentFName ="F:/asdf666.txt"Try: F= Open (fname,'W')     forElementinchelements:ifLen (Element.select ('H2')) >0:f.write (Element.select ('. Time') [0].text] F.write (Element.select ('H2') [0].text] F.write (Element.select ('a') [0]['href']) F.write ('\ n') F.close ()exceptException, E:PrinteElse:    Passfinally:    Pass

Because this is the first time to do the small reptile, the function is very simple and very single, is to crawl the news page part of the News

Then crawl the news time and hyperlinks

Then integrate them in the order of the news and put them into a text file to store them.

Using BeautifulSoup to crawl the content of Sina Web News

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.