Python's base crawler (using requests and BS4)

Source: Internet
Author: User

1. Online resources will be requested:

1 Import Requests 2 res=requests.get ('http://*******')3 res.encoding='  utf-8'4print(res.text)

This uses the requests get method to get the HTML, specifically get or post and so on through the page header information to query:

For example, Baidu's method is can use get get.

2, will get the Web page use BeautifulSoup to analyze

1  fromBs4ImportBeautifulSoup2Soup=beautifulsoup (Res.text,'Html.parser')3 Print(soup)#you can see the contents of the Web page4  forNewsinchSoup.select ('. News-item'):#Crawl Some news information5Header=news.select ('H1') [0].text#News Headlines6Time=news.select ('. Time') [0]#Time7     Print(Header,time)

This need to pay attention to the problem of the node, in the view of the source code of the page to distinguish the location of information storage, step by step to analyze, reasonable use for the loop.

Python's base crawler (using requests and BS4)

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.