-
https協(xié)議是帶小??標(biāo)記的是安全的
http沒有查看全部 -
? ? ? ? ? ? ? ? ? ? ? ? ? ?Xpathl路徑表達(dá)式
Xpath使用路徑表達(dá)式來選取XML文檔(或是HTML文檔)中的節(jié)點或節(jié)點集?
? ? ? ? ? ?路徑表達(dá)式:
? ? ? ? ? ? ? ? ? ? ? /div:從根節(jié)點開始選取div節(jié)點
? ? ? ? ? ? ? ? ? ? ?//a:選取文檔中所有的a節(jié)點而不考慮其位置
? ? ? ? ? ? ? ? ? ? @class:選取名為class的屬性
? ? ? ? ? ? ? ? ? ? ?.? :選取當(dāng)前節(jié)點
? ? ? ? ? ? ? ? ? ? ?..? :選取當(dāng)前節(jié)點的父節(jié)點
ctrl+shift+x 打開Xpath
/div/a :從根節(jié)點開始選取div節(jié)點下的a節(jié)點
/div/a[@class='header-wrapper' ]
查看全部 -
請求頭
查看全部 -
etree用來添加HTML和body標(biāo)簽
data?=?""" ??<div> ????<ul> ?????<li?class="item-0"><a?href="link1">first</a></li> ?????<li?class="item-in"><a?href="link2"><span?class="bold">second</span></a></li> ?????<li?class="item-0"><a?href="link3">third</a></li> ????</ul> ??</div> """ html?=?etree.HTML(data)
????
????2.獲取第一個li的a標(biāo)簽
1.print(html.xpath("http://li/a[@href='link1']")) 2.print(html.xpath("http://li[1]/a[1])
????3.獲取第二個li的span標(biāo)簽
1.#因為span是li的后代元素,所以要用兩個// 2.print(html.xpath("http://li//span"))
????4.獲取最后一個li里面的href(last的使用)
print(html.xpath("http://li[last()]/a/@href"))
查看全部 -
設(shè)置代理——隱藏請求
查看全部 -
第二遍沒太聽懂
查看全部 -
百度的返回頭如何設(shè)置 cookies
查看全部 -
百度的返回頭如何設(shè)置 cookies
查看全部 -
搜索
復(fù)制
查看全部 -
搜索
復(fù)制
查看全部 -
關(guān)閉校驗:verify=False
指定證書:verify='xxx'(某個路徑)
搜索
復(fù)制
查看全部 -
session信息
查看全部 -
Requests模塊構(gòu)造URL
查看全部 -
PUT方法
查看全部 -
HTTP POST方法用于提交數(shù)據(jù)(如表單)
查看全部 -
Head方法請求部分信息
查看全部 -
HTTP方法GET
查看全部 -
request是一個Python三方庫
查看全部 -
import?pymongo #?指定客戶端連接mongodb pymongo_client?=?pymongo.MongoClient("mongodb://localhost:27017") #?指定數(shù)據(jù)庫名稱 pymongo_db?=?pymongo_client["imooc"] #?創(chuàng)建集合,創(chuàng)建表 pymongo_collection?=?pymongo_db["pymongo_test"] data?=?{ ????"name":?"imooc", ????"flag":?1, ????"url":?"http://idcbgp.cn" } mylist?=?[ ????{"name":?"imooc",?"flag":?"123",?"url":?"http://idcbgp.cn"}, ????{"name":?"taobao",?"flag":?"456",?"url":?"http://idcbgp.cn"}, ????{"name":?"qq",?"flag":?"761",?"url":?"http://idcbgp.cn"}, ????{"name":?"知乎",?"flag":?"354",?"url":?"http://idcbgp.cn"}, ????{"name":?"微博",?"flag":?"954",?"url":?"http://idcbgp.cn"} ] #?pymongo_collection.insert_one(data) #?pymongo_collection.insert_many(mylist) #?result=pymongo_collection.find({},{"_id":0,"name":1,"url":1,"flag":1}) #?result=pymongo_collection.find({},{"_id":0,"name":1,"url":1,"flag":1}) #?result=pymongo_collection.find_one() #?print(result) #?for?item?in?result: #?????print(item) #?大于一百 #?find_result=pymongo_collection.find({"flag":{"$gt":100}}) #?首字母t #?find_result=pymongo_collection.find({"name":{"$regex":"^t"}}) #?for?item?in?find_result: #?????print(item) #?pymongo_collection.update_one({"name":{"$regex":"^t"}},{"$set":{"name":"baidu"}}) #?pymongo_collection.delete_one({"name":"微博"}) pymongo_collection.delete_many({})
查看全部 -
import?requests from?lxml?import?etree from?pymongo.collection?import?Collection import?pymongo print("開始0") class?Dangdang(object): ????mongo_client=pymongo.MongoClient(host="localhost",port=27017) ????dangdang_db=mongo_client["dangdang_db"] ????def?__init__(self): ????????self.header?=?{ ????????????"Host":?"bang.dangdang.com", ????????????"Connection":?"keep-alive", ????????????"Cache-Control":?"max-age=0", ????????????"Upgrade-Insecure-Requests":?"1", ????????????"User-Agent":?"Mozilla/5.0?(Windows?NT?10.0;?Win64;?x64)?AppleWebKit/537.36?(KHTML,?like?Gecko)?Chrome/91.0.4472.124?Safari/537.36", ????????????"Accept":?"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", ????????????"Referer":?"http://bang.dangdang.com/books/bestsellers/01.00.00.00.00.00-24hours-0-0-1-2", ????????????"Accept-Encoding":?"gzip,deflate", ????????????"Accept-Language":?"zh-CN,zh;q=0.9" ????????} ????????self.dangdang=Collection(Dangdang.dangdang_db,"dangdang") ????def?get_dangdang(self,?page): ????????"""發(fā)送請求到當(dāng)當(dāng)網(wǎng)獲取數(shù)據(jù)""" ????????url?=?"http://bang.dangdang.com/books/bestsellers/01.00.00.00.00.00-24hours-0-0-1-%s"?%?page ????????response?=?requests.get(url=url,?headers=self.header) ????????if?response: ????????????#?????????html數(shù)據(jù)實例化 ????????????#?print(response.text) ????????????html1?=?etree.HTML(response.content) ????????????items?=?html1.xpath("http://ul[@class='bang_list?clearfix?bang_list_mode']/li") ????????????return?items ????def?join_list(self,item): ????#?????處理列表→字符串 ????????return?"".join(item) ????def?parse_item(self,items): ????????#?????????解析條目 ????????#?存到mongodb之前的數(shù)據(jù) ????????result_list=[] ????????for?item?in?items: ????????????#?名稱 ????????????title=item.xpath(".//div[@class='name']/a/@title") ????????????#?圖書評論 ????????????comment=item.xpath(".//div[@class='star']/a/text()") ????????????#?作者信息 ????????????author=item.xpath(".//div[@class='publisher_info'][1]/a[1]/@title") ????????????#?價格 ????????????price=item.xpath(".//div[@class='price']/p[1]/span[1]/text()") ????????????result_list.append( ????????????????{ ????????????????????"title":self.join_list(title), ????????????????????"comment":self.join_list(comment), ????????????????????"author":self.join_list(author), ????????????????????"price":self.join_list(price) ????????????????} ????????????) ????????return?result_list ????def?insert_data(self,result_list): ????????self.dangdang.insert_many(result_list) def?main(): ????d?=?Dangdang() ????print("開始") ????import?json ????for?page?in?range(1,?26): ????????items?=?d.get_dangdang(page=page) ????????result=d.parse_item(items=items) ????????#?print(json.dumps(result)) ????????print(result) ????????#?d.insert_data(result) if?__name__?==?'__main__': ????main()
查看全部 -
注意單引號雙引號的問題
孫子標(biāo)簽用//
獲取最后一個li標(biāo)簽的a標(biāo)簽的href的值
查看全部 -
xpath
//div[@class="result c-container new-pmd"]
//dd[@class="lemmaWgt-lemmaTitle-title J-lemma-title"]/a[3]/text()
//div[not(contains(@class,"para"))]
//div[last()-2]
查看全部 -
Chrome商店 XPath Helper查看全部
-
遇到網(wǎng)絡(luò)問題: ConnectionError異常
如果HTTP請求返回了不成功的狀態(tài)碼: HTTPError異常
若請求超時,則拋出一個Timeout異常
若請求超過了設(shè)定的最大重定向次數(shù),則會拋出一個 TooManyRedirects異常
查看全部 -
查看全部
-
cookie
查看全部 -
#?隨便練習(xí) print(1)
查看全部
舉報