網路爬蟲的採集,處理,儲存

2021-07-16 15:21:41 字數 3569 閱讀 9967

爬蟲**:

#coding:utf-8

import _mysql,sys

import time

import socket

import random

import mysqldb

from queue import queue

from threading import thread

from bs4 import beautifulsoup

from selenium import webdriver

from selenium.webdriver.common.by import by

from selenium.webdriver.common.desired_capabilities import desiredcapabilities #設定請求頭

create_sql = """create table movie(id int(2) not null primary key auto_increment,

title varchar(200),

href text

)default charset=utf8;

"""user_agent_list = [

"mozilla/5.0 (windows nt 6.1; wow64; rv:47.0) gecko/20100101 firefox/47.0",

"mozilla/5.0 (windows nt 10.0; wow64; rv:48.0) gecko/20100101 firefox/48.0",

"mozilla/5.0 (windows nt 5.1; u; en; rv:1.8.1) gecko/20061208 firefox/2.0.0 opera 9.50"

"mozilla/4.0 (compatible; msie 6.0; windows nt 5.1; en) opera 9.50"

"mozilla/5.0 (windows nt 6.1; wow64; trident/7.0; rv:11.0) like gecko"

"mozilla/4.0 (compatible; msie 6.0; windows nt 5.1; sv1; qqdownload 732; .net4.0c; .net4.0e)"

"mozilla/4.0 (compatible; msie 7.0; windows nt 5.1; trident/4.0; sv1; qqdownload 732; .net4.0c; .net4.0e; se 2.x metasr 1.0)"

]def

store

(title,url):

cur.execute("insert into movie (title,href) values (%s,%s)",(title,url))

cur.connection.commit()

defget_source

(url):

#service_args = ['--proxy=localhost:9150','--proxy-type=sock5'] #**設定

dcap = dict(desiredcapabilities.phantomjs)

dcap["phantomjs.page.settings.useragent"] = (random.choice(user_agent_list))

driver = webdriver.phantomjs(executable_path=r'd:\phantomjs-2.1.1\bin\phantomjs.exe', desired_capabilities=dcap) #,service_args=service_args ip**設定

try:

driver.get(url)

except exception,e:

print

"此處出現異常,該異常資訊為:%s" % e

print

"正在努力再嘗試一次......"

driver.get(url)

time.sleep(5)

html = driver.page_source

driver.close()

try:

soup = beautifulsoup(html, 'html.parser')

a_list = soup.findall("a", )

a_page = soup.findall("a", )[0]

page = a_page.string

except exception,e:

print

"此處出現異常,該異常資訊為:%s" % e

print

"\n"

print

"第 " + str(page) + " 頁的電影集:\n\n"

for a_each in a_list:

movie_title = a_each.img['alt']

movie_href = '' + a_each['href']

print movie_title, movie_href

print

"正在向資料庫匯入電影資訊:"

try:

store(movie_title,movie_href)

except _mysql.error,e:

print("資料庫的error %d:%s" % (e.args[0], e.args[1]))

store(movie_title, movie_href)

print

"電影資訊匯入完畢"

if __name__ == "__main__":

conn = mysqldb.connect(host='127.0.0.1', user='root', passwd='123456', db='movie_info', port=3306,charset='utf8')

cur = conn.cursor() # cur 游標物件

cur.execute("use movie_info")

cur.execute("drop table movie")

cur.execute(create_sql)

q = queue()

for i in xrange(1,706):

newpage = "#!page=" + str(i)

q.put(newpage)

print

"\n電影爬蟲開始......\n"

for i in xrange(1,706):

t = thread(target=get_source, args=(q.get(),))

t.setdaemon(true)

t.start()

time.sleep(3)

#socket.setdefaulttimeout(50) # 設定10秒後連線超時

t.join()

print

"\n資料抓取完畢\n"

cur.close()

conn.close()

print

"總部電影集匯入資料庫完畢"

通過網路爬蟲採集大資料

在網際網路時代,網路爬蟲主要是為搜尋引擎提供最全面和最新的資料。在大資料時代,網路爬蟲更是從網際網路上採集資料的有利工具。目前已經知道的各種網路爬蟲工具已經有上百個,網路爬蟲工具基本可以分為 3 類。本節首先對網路爬蟲的原理和工作流程進行簡單介紹,然後對網路爬蟲抓取策略進行討論,最後對典型的網路工具...

Python 網路爬蟲(新聞採集指令碼)

爬蟲原理 通過python訪問新聞首頁,獲取首頁所有新聞鏈結,並存放至url集合中。逐一取出集合中的url,並訪問鏈結獲取原始碼,解析出新的url鏈結新增到集合中。為防止重複訪問,設定乙個歷史訪問,用於對新新增的url進行過濾。解析dom樹,獲取文章相關資訊,並將資訊儲存到article物件中。將a...

網路爬蟲(2) 儲存

目前我想到的儲存方案有兩種 單檔案單頁面儲存和單檔案多頁面儲存。單檔案單頁面儲存就是乙個頁面儲存在乙個檔案中,檔名稱可以使用頁面編號doc id。這種方案查詢的時候容易定位,直接使用檔名就可以,缺點是會產生大量瑣碎的檔案,管理 冗餘和查詢感覺都不太方便。單檔案多頁面儲存即乙個檔案儲存多個頁面,寫滿乙...