How beautiful is the universe on your birthday? One-click query pictures with Python making gadgets!

There is a hot search on Weibo these two days-the universe on your birthday, it looks very interesting, click on the link to find a website (to celebrate the 30th anniversary of the Hubble Telescope) provided by NASA (NASA), you can check it out A picture of space taken by the Hubble telescope on your birthday. However, it is probably due to too many people or some domestic factors, and the web page loads slowly. Therefore, Xiaobencong wrote a query software for everyone to experience.

Original link to WeChat official account

The required Python libraries are: PyQt5, requests, lxml, openpyxl, pillow, and some libraries that come with Python. First look at the interface:

How does it work?

1.  First of all, you can download an excel form on the website, the form is roughly like this:

v2-8cf439d092eaf846ddac21c2ac31d08d_r.jpg

It contains a link to the introduction page corresponding to the universe captured by the Hubble telescope on each date, so we need to read this table,

'''导入中的全年数据'''
def loadFullYearData(self, filepath):
   full_year_data = {}
   excel_data = load_workbook(filepath)
   sheet = excel_data.get_sheet_by_name('365')
   for idx, row in enumerate(sheet.rows):
       if idx > 366: break
       if idx > 0: full_year_data[row[0].value.strftime('%Y-%m-%d')[5:]] = row[4].value
   return full_year_data

2. Then according to the date entered by the user to obtain the corresponding introduction page link:

url = self.full_year_data.get(key)

3. After  obtaining the introduction page link, request it through the requests library, and use xpath to extract the data we need, that is, the photo link and the photo introduction, as shown in the red box below:

The path of xpath can be copied directly in the browser, and the code is implemented as follows:

res = requests.get(url, headers=headers)
html_root = etree.HTML(res.text)
html = html_root.xpath('//*[@id="main-content"]/section/section/div[1]/div/div/div[2]')[0].xpath('./p')
# 提取介绍
intro = []
for item in html:
    intro.append(item.xpath('text()')[0])
  # 提取图片链接并下载
  idx = -1
  while True:
     image_url = html_root.xpath('//*[@id="main-content"]/section/section/div[1]/div/div/div[1]/div/a')[idx]
     image_url = ('https:' + image_url.xpath('@href')[0]).replace('imgsrc.hubblesite.org/hvi', 'hubblesite.org')
     if image_url.split('.')[-1] == 'jpg':
         break
     idx -= 1

4.  Next, download it according to the image link (because the image loading on the website is too slow, so it is best to set up retry yourself):

filename = 'tmp.%s' % image_url.split('.')[-1]
f = open(filename, 'wb')
session = requests.Session()
retry = Retry(connect=10000, backoff_factor=0.5)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
res = session.get(image_url, headers=headers, stream=True, verify=False)
for chunk in res.iter_content(chunk_size=1024): f.write(chunk)
f.close()

5.  Finally, use PyQt5 to write a simple visual interface and add the crawler function. In addition, you can use the pyinstaller library to package the code into exe files for easy use. By the way, you can also practice how to use the pyinstaller module efficiently.

picture display:

The above is the process of this Python making picture query applet. WeChat public account " financial learner who learns programming " back-end " birthday pictures " to get the source code .

Original link to WeChat official account

Welcome attention to micro-channel public number: learn programming financial passenger, Author: Xiaoben Cong

 

Published 11 original articles · praised 11 · visited 5718

Guess you like

Origin blog.csdn.net/weixin_39270299/article/details/105224938