Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to climb the weather and broadcast it in language

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to use Python to climb the weather and broadcast the language". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Python to climb the weather and broadcast the language".

I. preliminary knowledge

This case realizes the function: use web crawlers to crawl the weather of a place, and print and voice broadcast. If you want to use requests library, lxml library and pyttsx3 library, if you don't have them, you can install them first. You can install them all through pip:

Pip install requests pip install lxml pip install pyttsx3

Requests library is a powerful network request library, which can send all kinds of HTTP requests to get the data of the website just like the browser.

The Lxml library is the most versatile and easy-to-use library for dealing with XML and HTML. Etree in the lxml library is usually used to convert HTML into documents.

Pyttsx3 library is a very simple library to play voice, what you give it, what it reads, of course, never mind the blunt tone. The basic usage is as follows:

Import pyttsx3 word = pyttsx3.init () word.say ('Hello') # key sentence, without this line of code, voice word.runAndWait () will not be played.

The codeword is not easy to talk nonsense: if you need learning materials or have technical problems to communicate, you can send a private message to the editor to send "01".

Crawler is to crawl the relevant content of the web page, understanding HTML can help you better understand the structure of the web page, content and so on. The knowledge of TCP/IP protocol and HTTP protocol can help you understand the basic principles of network request and network transmission, which is not needed in this small case.

2. Explain it in detail

2.1. Get request destination URL

We first import the requests library, and then use it to get the target web page. we request the Beijing weather in the weather website.

Import requests # sends a request to the destination url address and returns a response object req = requests.get ('https://www.tianqi.com/beijing/') # .text is the web page html print (req.text) of the response object)

The printed result is the content displayed on the website, through which the browser "parses" it. The structure we see is as follows:

The data obtained after our request

Note that it is very likely that friends will not get the web code after running it, but display 403 instead. What does this mean?

A 403 error is a common error message during a website visit, indicating that resources are not available. The server understands the customer's request but refuses to process it.

The crawlers we write generally tell the server by default that they send a Python crawl request, and many websites will set up an anti-crawler mechanism, which is not allowed to be accessed by the crawler.

So, if we want the target server to respond, let's camouflage our crawler. This small case is camouflaged with the commonly used change User-Agent field.

Change the previous code to disguise the crawler as a browser request so that normal access can be made.

Import requests headers = {'content-type':'application/json',' User-Agent':'Mozilla/5.0 (Xll; Ubuntu; Linux x86room64; rv:22.0) Gecko/20100101 Firefox/22.0'} # sends a request to the destination url address and returns a response object req = requests.get ('https://www.tianqi.com/beijing/',headers=headers) # .text is the web page html print (req.text) of the response object)

Where did the User-Agent field come from? We take Chrome browser as an example, first casually open a web page, press F12 of the keyboard or click the right mouse button in the blank to select "check"; then refresh the page, click "Network" and then click "Doc", click Headers, view the User-Agent field of Request Headers in the information bar, copy directly, and we can use it.

2.2. Lxml.etree debut

The data we get from the web page is complicated, and only part of it is the data we really want. For example, if we look at the weather in Beijing from the weather website, we can only get what we want in the picture below. How can we extract it? Lxml.etree will be used here.

There is only a small part of the information we want in the whole code, and we find that the desired weather and temperature are all below the "class='weather_info'" level, which is easy to do. We add the following to the requested code:

Html_obj = etree.HTML (html) html_data = html_obj.xpath ("/ / D1 [@ class='weather_info'] / / text ()")

Let's print (html_data) to see if the extraction is the data we want.

I found that even the newline characters in the web page have been extracted, and, don't forget, the list has been extracted. We have to deal with it.

Word = "Welcome to Weather Assistant" for data in html_data: word + = data

After dealing with it, let's print it and see, well, we have everything we want. However, there is one more [switching city], we strive for excellence, and finally get rid of this.

2.3. Tell the results.

All the data we want is in the word variable. Let him read it now and use the pyttsx3 library.

Ptt = pyttsx3.init () ptt.say (word) ptt.runAndWait ()

Okay, it's all done now. We fumbled step by step, now integrated together, the final playback effect is still good, this is a very beautiful crawler trip, looking forward to the next crawl!

Thank you for your reading, the above is the content of "how to use Python to climb the weather and broadcast in language". After the study of this article, I believe you have a deeper understanding of how to use Python to climb the weather and broadcast in language, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report