Python3 gets a lot of movie information: Call the API and python3 get the call api

Source: Internet
Author: User

Python3 gets a lot of movie information: Call the API and python3 get the call api

During this time in the lab, we need to collect information about movies and provide a large dataset. The dataset contains more than 4000 movie names. I need to write a crawler to crawl the information about movies corresponding to the movie name.

 

In fact, in actual operation, crawlers are not required at all. You only need a simple Python Foundation.

 

Prerequisites:

Python3 syntax Basics

HTTP network Basics

 

==========================================

Step 1: Determine the API provider. IMDb is the largest movie database. In contrast, an OMDb website provides APIs for use. The API of this website is very friendly and easy to use.

Http://www.omdbapi.com/

Step 2: Determine the URL format.

Step 3: learn how to use the basic Requests library.

Http://cn.python-requests.org/zh_CN/latest/

 

Why do I need to use Requests instead of urllib. request?

Because the Python library is prone to a variety of strange issues, I already have enough ......

 

Step 4: Write Python code.

What I want to do is read the file row by row and use the name of the row to obtain the information about the movie. Because the source file is large, readlines () cannot completely read all movie names, so we can read them row by row.

 1 import requests 2  3 for line in open("movies.txt"): 4     s=line.split('%20\n') 5     urll='http://www.omdbapi.com/?t='+s[0] 7     result=requests.get(urll) 8     if result: 9         json=result.text10         print(json)11         p=open('result0.json','a')12         p.write(json)13         p.write('\n')14         p.close()

I have formatted all the movie name files in advance and replaced all spaces with "% 20" to facilitate the use of API (otherwise, an error is reported ). This function can be completed using Visual Studio Code.

 

Note: When encoding, select GBK encoding. Otherwise, the following error will occur:

1 UnicodeDecodeError: 'gbk' codec can't decode byte 0xff in position 0: illegal multibyte sequence

 

Step 5: optimize and handle exceptions.

We mainly do three things: first, control the API speed to prevent blocking by the server;

The second thing is to get the API key (or even use multiple keys)

Third thing: handle exceptions.

 1 import requests 3  4 key=[‘’] 5  6 for line in open("movies.txt"): 7     try: 8         #…… 9     except TimeoutError:10         continue11     except UnicodeEncodeError:12         continue13     except ConnectionError:14         continue

 

The complete code is provided below:

 1 # -*- coding: utf-8 -*- 2  3 import requests 4 import time 5  6 key=['xxxxx','yyyyy',zzzzz','aaaaa','bbbbb'] 7 i=0 8  9 for line in open("movies.txt"):10     try:11         i=(i+1)%512         s=line.split('%20\n')13         urll='http://www.omdbapi.com/?t='+s[0]+'&apikey='+key[i]14         result=requests.get(urll)15         if result:16             json=result.text17             print(json)18             p=open('result0.json','a')19             p.write(json)20             p.write('\n')21             p.close()22             time.sleep(1)23     except TimeoutError:24         continue25     except UnicodeEncodeError:26         continue27     except ConnectionError:28         continue

Next, have a cup of tea and see how your program is running!

 

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.