如何用Python爬取当当、京东、亚马逊图书信息
发表于:2025-02-02 作者:千家信息网编辑
千家信息网最后更新 2025年02月02日,本篇内容主要讲解"如何用Python爬取当当、京东、亚马逊图书信息",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"如何用Python爬取当当、京东、亚马逊图
千家信息网最后更新 2025年02月02日如何用Python爬取当当、京东、亚马逊图书信息
本篇内容主要讲解"如何用Python爬取当当、京东、亚马逊图书信息",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"如何用Python爬取当当、京东、亚马逊图书信息"吧!
注:
1.本程序采用 MSSQLserver 数据库存储,请运行程序前手动修改程序开头处的数据库链接信息;
2.需要 bs4、requests、pymssql 库支持;
3.支持多线程。
源码
from bs4 import BeautifulSoup import re,requests,pymysql,threading,os,traceback try: conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='book',charset="utf8") cursor = conn.cursor() except: print('\\\\n错误:数据库连接失败') #返回指定页面的html信息 def getHTMLText(url): try: headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'} r = requests.get(url,headers = headers) r.raise_for_status() r.encoding = r.apparent_encoding return r.text except: return '' #返回指定url的Soup对象 def getSoupObject(url): try: html = getHTMLText(url) soup = BeautifulSoup(html,'html.parser') return soup except: return '' #获取该关键字在图书网站上的总页数 def getPageLength(webSiteName,url): try: soup = getSoupObject(url) if webSiteName == 'DangDang': a = soup('a',{'name':'bottom-page-turn'}) return a[-1].string elif webSiteName == 'Amazon': a = soup('span',{'class':'pagnDisabled'}) return a[-1].string except: print('\\\\n错误:获取{}总页数时出错...'.format(webSiteName)) return -1 class DangDangThread(threading.Thread): def __init__(self,keyword): threading.Thread.__init__(self) self.keyword = keyword def run(self): print('\\\\n提示:开始爬取当当网数据...') count = 1 length = getPageLength('DangDang','http://search.dangdang.com/?key={}'.format(self.keyword))#总页数 tableName = 'db_{}_dangdang'.format(self.keyword) try: print('\\\\n提示:正在创建DangDang表...') cursor.execute('create table {} (id int ,title text,prNow text,prPre text,link text)'.format(tableName)) print('\\\\n提示:开始爬取当当网页面...') for i in range(1,int(length)): url = 'http://search.dangdang.com/?key={}&page_index={}'.format(self.keyword,i) soup = getSoupObject(url) lis = soup('li',{'class':re.compile(r'line'),'id':re.compile(r'p')}) for li in lis: a = li.find_all('a',{'name':'itemlist-title','dd_name':'单品标题'}) pn = li.find_all('span',{'class': 'search_now_price'}) pp = li.find_all('span',{'class': 'search_pre_price'}) if not len(a) == 0: link = a[0].attrs['href'] title = a[0].attrs['title'].strip() else: link = 'NULL' title = 'NULL' if not len(pn) == 0: prNow = pn[0].string else: prNow = 'NULL' if not len(pp) == 0: prPre = pp[0].string else: prPre = 'NULL' sql = "insert into {} (id,title,prNow,prPre,link) values ({},'{}','{}','{}','{}')".format(tableName,count,title,prNow,prPre,link) cursor.execute(sql) print('\\\\r提示:正在存入当当数据,当前处理id:{}'.format(count),end='') count += 1 conn.commit() except: pass class AmazonThread(threading.Thread): def __init__(self,keyword): threading.Thread.__init__(self) self.keyword = keyword def run(self): print('\\\\n提示:开始爬取亚马逊数据...') count = 1 length = getPageLength('Amazon','https://www.amazon.cn/s/keywords={}'.format(self.keyword))#总页数 tableName = 'db_{}_amazon'.format(self.keyword) try: print('\\\\n提示:正在创建Amazon表...') cursor.execute('create table {} (id int ,title text,prNow text,link text)'.format(tableName)) print('\\\\n提示:开始爬取亚马逊页面...') for i in range(1,int(length)): url = 'https://www.amazon.cn/s/keywords={}&page={}'.format(self.keyword,i) soup = getSoupObject(url) lis = soup('li',{'id':re.compile(r'result_')}) for li in lis: a = li.find_all('a',{'class':'a-link-normal s-access-detail-page a-text-normal'}) pn = li.find_all('span',{'class': 'a-size-base a-color-price s-price a-text-bold'}) if not len(a) == 0: link = a[0].attrs['href'] title = a[0].attrs['title'].strip() else: link = 'NULL' title = 'NULL' if not len(pn) == 0: prNow = pn[0].string else: prNow = 'NULL' sql = "insert into {} (id,title,prNow,link) values ({},'{}','{}','{}')".format(tableName,count,title,prNow,link) cursor.execute(sql) print('\\\\r提示:正在存入亚马逊数据,当前处理id:{}'.format(count),end='') count += 1 conn.commit() except: pass class JDThread(threading.Thread): def __init__(self,keyword): threading.Thread.__init__(self) self.keyword = keyword def run(self): print('\\\\n提示:开始爬取京东数据...') count = 1 tableName = 'db_{}_jd'.format(self.keyword) try: print('\\\\n提示:正在创建JD表...') cursor.execute('create table {} (id int,title text,prNow text,link text)'.format(tableName)) print('\\\\n提示:开始爬取京东页面...') for i in range(1,100): url = 'https://search.jd.com/Search?keyword={}&page={}'.format(self.keyword,i) soup = getSoupObject(url) lis = soup('li',{'class':'gl-item'}) for li in lis: a = li.find_all('div',{'class':'p-name'}) pn = li.find_all('div',{'class': 'p-price'})[0].find_all('i') if not len(a) == 0: link = 'http:' + a[0].find_all('a')[0].attrs['href'] title = a[0].find_all('em')[0].get_text() else: link = 'NULL' title = 'NULL' if(len(link) > 128): link = 'TooLong' if not len(pn) == 0: prNow = '¥'+ pn[0].string else: prNow = 'NULL' sql = "insert into {} (id,title,prNow,link) values ({},'{}','{}','{}')".format(tableName,count,title,prNow,link) cursor.execute(sql) print('\\\\r提示:正在存入京东网数据,当前处理id:{}'.format(count),end='') count += 1 conn.commit() except : pass def closeDB(): global conn,cursor conn.close() cursor.close() def main(): print('提示:使用本程序,请手动创建空数据库:Book,并修改本程序开头的数据库连接语句') keyword = input("\\\\n提示:请输入要爬取的关键字:") dangdangThread = DangDangThread(keyword) amazonThread = AmazonThread(keyword) jdThread = JDThread(keyword) dangdangThread.start() amazonThread.start() jdThread.start() dangdangThread.join() amazonThread.join() jdThread.join() closeDB() print('\\\\n爬取已经结束,即将关闭....') os.system('pause') main()
示例截图:
关键词:Android 下的部分运行结果(以导出至 Excel )
到此,相信大家对"如何用Python爬取当当、京东、亚马逊图书信息"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
提示
数据
京东
亚马
亚马逊
正在
信息
数据库
程序
图书
页数
页面
关键
处理
关键字
内容
开头
当当网
手动
错误
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
文明上网守护网络安全ppt
java数据库怎么建
如何修改服务器时间和网络一致
orm数据库开发
刘家沟湾煤矿网络安全培训课
阿里云 网络安全组
数据库转移工具
课程软件开发合作协议
网络安全督导检查情况反馈
中国物联网服务器何时启动
方舟服务器出租
ktsk服务器故障码err60
飞鱼星 服务器
innodb恢复数据库
计算机网络技术试题调研
sql2016数据库备份到群晖
数据库 添加属性
腾讯云服务器如何更改配置
中国5g网络技术由哪家公司提供
网络安全等级保护20全文
apk软件开发.pdf
hdf5数据库优缺点
软件出版框架下软件开发
鹤壁电商财务软件开发
问道手游最新服务器
开发软件和嵌入式软件开发
蔡甸专业的软件开发价格
电脑刷机无法连接苹果服务器
阳江无限软件开发平均价格
电力信息网络技术