正则爬取京东商品信息并打包成.exe可执行程序。

2022-10-17,,,,

本文爬取内容,输入要搜索的关键字可自动爬取京东网站上相关商品的店铺名称,商品名称,价格,爬取100页(共100页)

代码如下;

import requests
import re
# 请求头
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)
            # 转码
            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('店铺名称:' + i[2]+'\n'+'商品名称:'+i[1]+'\n'+'价格:'+i[0]+'\n\n')
                print('第'+str(num)+'页'+'数据下载中....')
        except exception as e:
            print(e)


if __name__ == '__main__':
    print('输入要搜索的内容,获取京东商城里面的商品名称,店铺名称,商品价格')
    key = input('输入搜索内容:')
    url = 'https://search.jd.com/search?'
    get_all(url,key)

打包成.exe可执行文件。

需要用到pyinstaller包pip下载;

pip install pyinstaller

在线制作一个.ico图标,用来当程序图片,把图标和程序放在同一个文件夹下,

 

 

 

 

在.py文件目录下打开命令行窗口,执行打包命令;

e:\练习\最后阶段\0808\jd1>pyinstaller -f -i dog.ico jd.py

出现successfully表示打包成功;

27525 info: building exe from exe-00.toc completed successfully.

可执行程序在当前文件夹下的dist文件夹下;

运行效果;

可同时执行多个程序;

输出结果;

done。

 

《正则爬取京东商品信息并打包成.exe可执行程序。.doc》

下载本文的Word格式文档,以方便收藏与打印。