赞
踩
基于搜索结果,我将为您提供一个关于当当图书网数据采集的文章框架,假设我们已经有了10万条数据的采集结果。请注意,由于没有具体的数据文件,以下内容将是一个示例性的框架,您可以根据实际采集到的数据进行填充和调整。
在数字化时代,数据成为了理解和预测市场趋势的关键。本文将展示如何通过数据采集技术从当当图书网获取10万条图书数据,并对其进行分析,以揭示图书市场的一些有趣现象和趋势。
通过对10万条当当图书网数据的采集和分析,我们能够深入理解当前图书市场的动态,为出版社、作者和读者提供有价值的市场洞察。
def get_page_tree(url): from requests.adapters import HTTPAdapter page = requests.session() page.mount('http://',HTTPAdapter(max_retries=80)) page.mount('https://',HTTPAdapter(max_retries=80)) page = page.request("GET",url=url,timeout=120) print('page ' , page ,page.text) tree = etree.HTML(page.text) return tree # xpath语法 获取li对象 因为li标签不只一个 所以用lis def get_lis(tree): lis = tree.xpath('//*[@id="search_nature_rg"]/ul/li') return lis def deal_save(url , pressN,page_index): global sum global row tree = get_page_tree(url) # 获取到lis对象 lis = get_lis(tree) # time.sleep(random.randint(1,3)) print("正在爬取...", url) # 遍历lis 返回index下标和item try: for c, i in enumerate(lis): # 根据xpath规则提取图书标题 name = i.xpath('a/@title') name = str(name[0]) name = name.strip() # 根据xpath规则提取图书描述 desc = i.xpath('p[2]/text()') # 根据xpath规则提取图书价格和该书电子书的价格 price_normal = i.xpath('p[3]/span[1]/text()') # 纸质书的价格 price_normal = price_normal[0] price_net = i.xpath('p[3]/a/i/text()') # 电子书的价格 # 根据xpath规则提取图书的作者 author = i.xpath('p[@class="search_book_author"]/span[1]/a/text()') # 作者 if len(author) == 0: author = '没有作者' else: author = author[0] # 根据xpath规则提取图书出版时间 press_time = i.xpath('p[@class="search_book_author"]/span[2]/text()') if len(press_time) == 0: press_time = '出版时间为空' else: press_time = press_time[0][2:] # 根据xpath规则提取图书出版社 press = i.xpath('p[@class="search_book_author"]/span[3]/a/text()') if len(press) == 0: press = '出版社为空' else: press = press[0] if len(desc) != 0: desc = desc[0] else: desc = "null" if len(price_net) != 0: price_net = price_net[0] else: price_net = "暂无电子书" sum += 1 # 输出信息 print('第{}页--第{}条数据--累计爬取{}条:'.format(page_index, c, sum)) print('书名:《{}》'.format(name)) print('图书价格:{}'.format(price_normal)) print('电子书价格:{}'.format(price_net)) print('作者:{}'.format(author)) print('出版社:{}'.format(press)) print('出版时间:{}'.format(press_time)) print('*' * 25) time.sleep(0.1) # 保存信息 sheet.write(row, 0, name) sheet.write(row, 1, desc) sheet.write(row, 2 , price_normal) sheet.write(row, 3 , price_net) sheet.write(row, 4 , author) sheet.write(row, 5 , press_time) sheet.write(row, 6 , press) row = row + 1 except Exception as e: print(e) # 主要爬虫程序 遍历给定出版社 所有的图书 def start (url , pressN): # 获取page的etree new_url = url.format(press_name, 1) tree = get_page_tree(new_url) # 获取该出版社展示图书总页数 total = tree.xpath('//*[@id="12810"]/div[5]/div[2]/div/ul/li[9]/a/text()')[0] print('{}一共{}页'.format(pressN,total)) # 全局变量的sum 总数 global sum # time.sleep(2) # 线程保存的数组 threads = [ ] # 遍历所有页面 从1到total for page_index in range(1, int(total) + 1): time.sleep(0.2) temp_url = url.format(pressN,page_index) temp_t = Thread(target=deal_save , args = ( temp_url,press_name,page_index, )) threads.append(temp_t) temp_t.start() for index, t in enumerate(threads): t.join() if __name__ == '__main__': # 通过file读取所有出版社 保存为list数组 s = time.time() press = [] with open('Press.txt' , 'r' , encoding='utf-8') as f : a = f.read() f.close() # 拆封数据 press = a.split('\n') url = 'http://search.dangdang.com/?key={}&page_index={}' # 遍历所有出版社 for press_name in press: start(url , press_name) print('正在采集{}'.format(press_name)) end = time.time() book.save('当当网图书汇总.xlsx') print( '共花费:' ,str((end-s) ) , 's ,共计爬取:{}条'.format(sum))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。