非同步是過程,非阻塞強調的是狀態
from pymongo import mongoclient
# client = mongoclient(host=, port=)
uri = 'mongodb://賬號:密碼@127.0.0.1'
client = mongoclient(uri, port=27017) # 連線物件
# col = client['資料庫名']['集合名']'
col = client.資料庫名.集合名
col.insert(/[{}, {}, ...])
col.find_one()
rets = col.find() # 返回的是只能遍歷一次的cursor游標物件
col.delete_one()
col.delete_many()
col.update(,
},multi=false/true, # 預設false表示只更新一條
upsert=false/true) # 預設false,true表示沒有就插入,存在就更新
建立專案 scrapy startproject 專案名
在專案路徑下建立爬蟲 scrapy genspider 爬蟲名 爬取範圍的網域名稱
在專案路徑下執行爬蟲 scrapy crawl 爬蟲名
class spider(scrapy.spider):
name = 爬蟲名
allowed_domains = ['爬取範圍的網域名稱', '可以是多個']
start_urls = ['起始的url', '可以是多個']
# scrapy.spider類必須有名為parse的解析函式
def parse(self, response):
# 專門解析起始url對應的response
yield item # {} baseitem request none
response.xpath(xpath_str) # 返回由selector物件構成的類list
response.xpath(xpath_str).extract() # 返回包含字串的列表
response.xpath(xpath_str).extract_first() # 返回列表中第乙個字串
response.url
response.request.url
response.headers
response.request.headers
response.status
response.body # 響應內容 bytes
三個內建資料物件
request: url headers method post_data
response: url headers status body
item: {}/baseitem
五大模組
scheduler排程器
spider爬蟲
pipeline管道
engine引擎
兩個中介軟體:對request、response預處理
spider_middlewares爬蟲中介軟體
a. spider中把start_url構造成request
b. request--爬蟲中介軟體--引擎--排程器,把request放入請求佇列
e. spider對response提取,提取url,構造成request--重複b步驟
f. spider對response提取,提取item--引擎--管道
簡單爬蟲總結
url url主要有三部分組成 1 協議,常見的協議有http,https,ftp,file 訪問本地資料夾 ed2k 電驢的專用鏈結 等等。2 存放資源的伺服器的網域名稱系統 dns 主機名或者ip位址 有時候包含埠號,各種傳輸協議都有預設的埠號 3 主機資源的具體位址,如目錄和檔名等 注意 第一...
爬蟲總結(一)
requests模組 response urllib2.urlopen 讀取html原始碼使用read方法 html response.read 1.構造request物件 get請求 url 這種request是get請求,因為沒有給data傳值,如果需要使用get方式傳參,可以把引數經過urll...
爬蟲總結3
div id xx last a 2 href id是xx的div的父一級標籤下的所有標籤中最後乙個標籤下的第二個a標籤的名為href屬性的值 html a text text html下文字內容是 的所有a標籤下的當前標籤 就還是那個a標籤 的文字內容from lxml import etree ...