Week2_Retrieve and Store Information from Web with MongoBD

This project is to crawl a big amount of web links using the "crawler" and store them into MongoDB. Next, to retrieve the links from the MongoDB and retrieve the details from the links. For this project, I am reviewing how to crawl data from web and I am totally new to learn how to use MongoDB to store and filter data. I am also quite new to learn how to "if__name__== 'main':" to initiate a program.

We distributed the codes into 4 parts. The first one 'channel_extracing.py' is to retrieve the links from the web and store them into MongoDB. Below is the code.
<code>

import requests
from bs4 import BeautifulSoup

start_url = "http://bj.ganji.com/wu/"
base_url = "http://bj.ganji.com"

wb_data = requests.get(start_url)
soup = BeautifulSoup(wb_data.text,'lxml')
info_list = soup.select("dt > a")
for info in info_list:
url = base_url + info.get('href')
print(url)

channel_list = """
http://bj.ganji.com/shouji/
http://bj.ganji.com/shoujihaoma/
http://bj.ganji.com/shoujipeijian/
http://bj.ganji.com/bijibendiannao/
http://bj.ganji.com/taishidiannaozhengji/
http://bj.ganji.com/diannaoyingjian/
http://bj.ganji.com/wangluoshebei/
http://bj.ganji.com/shumaxiangji/
http://bj.ganji.com/youxiji/
http://bj.ganji.com/xuniwupin/
http://bj.ganji.com/jiaju/
http://bj.ganji.com/jiadian/
http://bj.ganji.com/zixingchemaimai/
http://bj.ganji.com/rirongbaihuo/
http://bj.ganji.com/yingyouyunfu/
http://bj.ganji.com/fushixiaobaxuemao/
http://bj.ganji.com/meironghuazhuang/
http://bj.ganji.com/yundongqicai/
http://bj.ganji.com/yueqi/
http://bj.ganji.com/tushu/
http://bj.ganji.com/bangongjiaju/
http://bj.ganji.com/wujingongju/
http://bj.ganji.com/nongyongpin/
http://bj.ganji.com/xianzhilipin/
http://bj.ganji.com/shoucangpin/
http://bj.ganji.com/baojianpin/
http://bj.ganji.com/laonianyongpin/
http://bj.ganji.com/gou/
http://bj.ganji.com/qitaxiaochong/
http://bj.ganji.com/xiaofeika/
http://bj.ganji.com/menpiao/
http://bj.ganji.com/jiaju/
http://bj.ganji.com/rirongbaihuo/
http://bj.ganji.com/shouji/
http://bj.ganji.com/shoujihaoma/
http://bj.ganji.com/bangong/
http://bj.ganji.com/nongyongpin/
http://bj.ganji.com/jiadian/
http://bj.ganji.com/ershoubijibendiannao/
http://bj.ganji.com/ruanjiantushu/
http://bj.ganji.com/yingyouyunfu/
http://bj.ganji.com/diannao/
http://bj.ganji.com/xianzhilipin/
http://bj.ganji.com/fushixiaobaxuemao/
http://bj.ganji.com/meironghuazhuang/
http://bj.ganji.com/shuma/
http://bj.ganji.com/laonianyongpin/
http://bj.ganji.com/xuniwupin/
http://bj.ganji.com/qitawupin/
http://bj.ganji.com/ershoufree/
http://bj.ganji.com/wupinjiaohuan/
"""
</code>

The second part is to get details for a single item from one link. The links are from the MongoDB. Here is the code "page_parsing.py":
<code>

import requests
from bs4 import BeautifulSoup
import time
import pymongo
import random

client = pymongo.MongoClient('localhost', 27017)
ganji = client['ganji']
item_url = ganji['item_url']
item_info = ganji['item_info']

headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36'
'Connection':'keep-alive'
}

proxy_list = [
'http://117.177.250.151:8081',
'http://111.85.219.250:3129',
'http://122.70.183.138:8118',
]
proxy_ip = random.choice(proxy_list)
proxies = {'http': proxy_ip}

def get_links_from(channel, pages):
page_link = channel + 'o{}'.format(str(pages))
wb_data = requests.get(page_link, headers=headers, proxies=proxies)
soup = BeautifulSoup(wb_data.text, 'lxml')
if soup.find('td', 't'):
urls = soup.select("td.t > a")
for url_sub in urls:
url = url_sub.get('href').split('?')[0]
print(url)
item_url.insert_one(url)
else:
pass

def get_item_info_from(url):
wb_data = requests.get(url, headers=headers)
if wb_data.status_code == '404':
pass
else:
soup = BeautifulSoup(wb_data.text, 'lxml')
titles = soup.select('h1.info_titile')
prices = soup.select('span.price_now > i')
places = soup.select('div.palce_li > span > i')
for title, price, place in zip(titles, prices, places):
data = {
'url': url,
'title': title.get_text(),
'price': price.get_text(),
'place': place.get_text()
}
print(data)
item_info.insert_one(data)

get_item_info_from('http://zhuanzhuan.ganji.com/detail/811531368570765314z.shtml')
</code>

Now is the "main.py" code. We use this to initiate the program. The code is below:
<code>

from multiprocessing import Pool
from channel_extracing import channel_list
from page_parsing import get_links_from, get_item_info_from, item_url, item_info

def get_all_links(channel):
for page in range(1,101):
get_all_links(channel,page)

if name == 'main':
pool = Pool()
pool.map(get_all_links,channel_list.split())
</code>

The last part is a separate code named "counts.py". Its function is to count every 5 seconds about how many information we have got so far. We run it apart from the main code (the above 3 ones). Here is the code:
<code>

import time
from page_parsing import url_list_v1

while True:
print(url_list_v1.find().count())
time.sleep(5)
</code>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,108评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,699评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,812评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,236评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,583评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,739评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,957评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,704评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,447评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,643评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,133评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,486评论 3 256
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,151评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,889评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,782评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,681评论 2 272

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,103评论 18 139
  • 从几何时,我们不再言语不再沟通,你我中间隔了2条距离,不知是什么,总会都有一种不想接话的尴尬! 我想男人和女人的区...
    爱吹风的妖妖阅读 141评论 0 1
  • 大家好,我是家雯妈妈。发现家里关于爸爸的书不是很多,这次买来了一套关于女儿和爸爸的书,图中的好多场景值得我们去探索...
    家雯妈妈阅读 1,211评论 0 1
  • 前几日失眠,躺在床上翻来覆去很久都没能睡着。宿舍内有些嘈杂,所以带上了耳机,想借着音乐来快些进入睡眠。 我想我可能...
    皮皮昕阅读 400评论 9 3
  • 放假前我班搞了一次班级的宿舍杯篮球赛。比赛完成之后,我列出了获奖的名单,承诺在放假之后再发奖品。今天中午我出...
    七哥特阅读 529评论 0 0