Regularized crawl product information and packaged it into.exe executable program.

Keywords: Python pip encoding Windows

This article crawls the content, enters the search key, can automatically climb the shop name, the commodity name, the price, and climb 100 pages (100 pages).

The code is as follows.

import requests
import re
# Request header
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
}

def get_all(url,key):
    for page in range(1,200,2):
        params = {
            'keyword':key,
            'enc':'utf-8',
            'page':page
        }
        num = int((int(page)+1)/2)
        try:
            response = requests.get(url=url,params=params,headers=headers)
            # transcoding
            content = response.text.encode(response.encoding).decode(response.apparent_encoding)
            data_all = re.findall('<div class="p-price">.*?<i>(.*?)</i>.*?<div class="p-name p-name-type-2">.*?title="(.*?)"'
                                  '.*?<div class="p-shop".*?title="(.*?)"',content,re.S)
            for i in data_all:
                with open(key + '.txt', 'a+', encoding='utf-8') as f:
                    f.write('Shop name:' + i[2]+'\n'+'Commodity Name:'+i[1]+'\n'+'Price:'+i[0]+'\n\n')
                print('The first'+str(num)+'page'+'Data Download....')
        except Exception as e:
            print(e)


if __name__ == '__main__':
    print('Enter the content to search, get the name of the commodity store, shop name, commodity price.')
    key = input('Enter the search content:')
    url = 'https://search.jd.com/Search?'
    get_all(url,key)

Packed into. exe executable file.

You need to download the pyinstaller package pip.

pip install pyinstaller

Make an. ico icon online, which can be used as a program picture. Place the icon and the program in the same folder.

 

 

 

 

Open the command line window in the. py file directory and execute the packing command.

E: Exercise Last Stage jd1 > pyinstaller-F-i dog.ico jd.py

successfully means successful packaging.

27525 INFO: Building EXE from EXE-00.toc completed successfully.

The executable program is in the dist folder under the current folder.

Operation effect;

It can execute multiple programs at the same time.

Output results;

done.

Posted by MajusC00L on Sat, 05 Oct 2019 12:29:06 -0700