Python3 Crawler Learning

Source: Internet
Author: User

Learned a few days python3, found that the current learning and crawler still not very related, so now ready to crawl and language synchronization learning.

2016.8.9 Night

From the simplest start, crawl all the contents of the specified URL:

# Encoding:utf-8 Import  "http://www.selflink.cn/selflink"== Data.decode ('  UTF-8')print(data)
# Encoding:utf-8 Import  "http://www.cma.gov.cn/"== Data.decode ('GBK  ')print(data)

Coding is different, one is Utf-8, one is GBK

In addition, when the output is written to a file, if you write an HTML file, open may be garbled, this time do not doubt the Chinese compatibility of Python! (Python Chinese compatibility super good) can actually use Notepad to open a crawl to the file, the encoding is wrong, you can save as a bit, reset the code (such as set to Utf-8), and then open the Web page found not garbled. For example, this code:

ImportUrllib.requesturl="http://www.douban.com/"webpage=urllib.request.urlopen (URL) data=webpage.read () data= Data.decode ('Utf-8') F= Open ("d:/1.html","W") f.write (data)Print(Type (webpage))Print(Webpage.geturl ())Print(Webpage.info ())Print(Webpage.getcode ())

Because Python stores files in ASCII code by default, the file crawled in the browser shows garbled characters. You can manually modify the encoding of the file.

Of course, if you want to set the file encoding automatically, you need to use the codecs library:

Importurllib.requestImportCodecsurl="http://www.douban.com/"webpage=urllib.request.urlopen (URL) data=webpage.read () data= Data.decode ('Utf-8') F= Codecs.open ("d:/1.html","W","Utf-8") f.write (data)Print(Type (webpage))Print(Webpage.geturl ())Print(Webpage.info ())Print(Webpage.getcode ())

Python3 Crawler Learning

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.