温馨提示:文末有 CSDN 平台官方提供的学长 QQ 名片 :)
本项目利用网络爬虫技术从某蔬菜网采集所有农产品的价格数据,包括北京、上海、安徽、湖北等全国所有省和直辖市的农产品价格数据,解析后存储到数据库中。 建立农产品价格数据仓库,以web交互形式对外提供检索服务,并利用 echarts 实现农产品的可视化分析。
基于大数据的农产品价格信息监测分析系统的主要功能包括:
针对某农产品信息网站,利用 request + beautifulsoup 编写原生网络爬虫,完成数据的采集和清洗,并存储到数据库或文件系统中:
- for alink in alinks:
- sheng_urls = {}
- for sheng in sheng_code:
- product_url = 'http://www.xxxxxx.com/{}/m12d-1cta{}by-1p{}.html'.format(alink['href'], sheng_code[sheng], '{}')
- sheng_urls[sheng] = product_url
-
- product = alink.text[:-2]
- product_sheng_urls[product] = sheng_urls
-
- for product in product_sheng_urls:
- for sheng in product_sheng_urls[product]:
- pro_sheng_count = 0
- print('抓取 {} {} 的价格数据'.format(sheng, product))
-
- base_url = product_sheng_urls[product][sheng]
- for page in range(1, 1000):
- try:
- url = base_url.format(page)
- resp = requests.get(url)
- resp.encoding = 'utf8'
- soup = BeautifulSoup(resp.text, 'lxml')
- table = soup.select('table.m_t_5')[0]
- table = table.find('table')
- trs = table.find_all('tr')
-
- for tr in trs:
- tds = tr.find_all('td')
- name = tds[0].text
- low_price = float(tds[2].text[1:].strip())
- high_price = float(tds[3].text[1:].strip())
- mean_price = float(tds[4].text[1:].strip())
- pub_time = tds[5].text
-
- product_info = [product, sheng, name, low_price, high_price, mean_price, pub_time]
- insert_product_infos.append(product_info)
- pro_sheng_count += 1
-
- if len(insert_product_infos) % 10 == 0:
- cursor.executemany(insert_sql, insert_product_infos)
- conn.commit()
- insert_product_infos.clear()
- except:
- pass
-
- # 获取最多的页数
- try:
- max_page = int(soup.find('div', attrs={'id': 'pager'}).span.b.text)
- if max_page == page:
- break
- time.sleep(0.5)
- except:
- break
-
- print('共计 {} 条'.format(pro_sheng_count))




本项目利用网络爬虫技术从某蔬菜网采集所有农产品的价格数据,包括北京、上海、安徽、湖北等全国所有省和直辖市的农产品价格数据,解析后存储到数据库中。 建立农产品价格数据仓库,以web交互形式对外提供检索服务,并利用 echarts 实现农产品的可视化分析。
欢迎大家点赞、收藏、关注、评论啦 ,由于篇幅有限,只展示了部分核心代码。
技术交流认准下方 CSDN 官方提供的学长 QQ 名片 :)
精彩专栏推荐订阅:
