爬蟲notes

‘’’

爬取思路:
1、requests(url)
2、requests + json
3、requests + XPath
4、requests + BeautifulSoup
5、selenium
6、scrapy框架
7、scrapy-redis 及分布式

===============================================
OS:
import os
os.system(“C: && p.txt”)
os.system(“ping 127.0.0.1”)

===============================================
requests:
requests.get(url, headers=headers, data={’’:’’}, proxies=proxies)

===============================================
Proxies:
proxies = {‘http’: ‘124.207.82.166:8008’} # 47.98.129.198
response = requests.get(request_url, proxies=proxies) # 發起請求

===============================================
File:
with open(path,‘w’) as f:
f.write(text)

===============================================
Threading:
import threading
threading.Thread(target=fun, kwargs={‘list_url’:list_url,‘path_order’:path_order1}).start()

===============================================
requests、json:
1.data = json.load(open(“package1.json”,encoding=“utf-8”))
response = requests.get(url, headers=headers)
print(response.text)

2.response = requests.get(url)
data = response.text
obj = json.loads(data)

===============================================
requests、XPath
from lxml import etree
response = requests.get(list_url, headers=headers)
content = response.content
selector = etree.HTML(scontent) # 將頁面裝入etree樹
items = selector.xpath(path_order) # 按照XPath查找樹,返回迭代,
title = item.xpath("./div/p[1]/a/text()")[0].strip() # 迭代對象item可繼續用XPath查找

===============================================
requests、BeautifulSoup
from bs4 import BeautifulSoup
response = requests.get(url)
html= response.text
soup = BeautifulSoup(html, ‘lxml’)
soup_str = soup.prettify() # 標準化html
tag = soup.b
tag的一系類操作

===============================================
selenium: 安裝對應chrome版本的 Selenium driver https://www.cnblogs.com/JHblogs/p/7699951.html
并且安裝依賴庫 pip install selenium
from selenium import webdriver
chromedriver = “G:/4Anaconda/chromedriver.exe” # 驅動若在python路徑下 即可省略這一步
browser = webdriver.Chrome(chromedriver)
#打開一個網頁
browser.get(“http://www.baidu.com”)
browser.find_element_by_id(“kw”).send_keys(“selenium”)
browser.find_element_by_id(“su”).click()
browser.title
browser.set_window_size(480, 800) #參數數字為像素點
browser.back()
browser.forward()
#退出并關閉窗口的每一個相關的驅動程序

browser.quit()

#關閉當前窗口
#browser.close()

隱式等待

from selenium import webdriver

browser = webdriver.Chrome()

這里用implicitly_wait()實現了隱式等待

browser.implicitly_wait(10)
browser.get(‘https://www.zhihu.com/explore’)
input = browser.find_element_by_class_name(‘zu-top-add-question’)
print(input)

顯示等待
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

browser = webdriver.Chrome()
browser.get(‘https://www.taobao.com/’)
wait = WebDriverWait(browser, 10)
input = wait.until(EC.presence_of_element_located((By.ID, ‘q’)))
button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ‘.btn-search’)))
print(input, button)

‘’’

‘’’
#一、 創建scrapy項目(cmd):
scrapy startproject weibospider
cd weibospider

    #二、 創建sipder語句cmd:scrapy genspider WeiboSpider image.baidu.com拒絕爬蟲協議 ROBOTSTXT_OBEY = False 運行爬蟲 scrapy crawl baiduimg#三、 設置數據結構name = scrapy.Field()#四、 導入 數據 from hotnewsSpider.items import WeiboSpiderItem使用 weiboitem = WeiboSpiderItem()weiboitem['name'] = '123'返回 yield weiboitem#五、 發送請求傳遞 (在parse中)yield scrapy.Request(url=url, headers=self.headers, cookies=self.cookies, callback=self.clickFindMore)# 發送請求傳遞并回調,加參 callbackyield scrapy.Request(link,callback=self.parse_detail)#六、重寫初始化請求def start_requests(self):for url in self.urls:yield scrapy.Request(url=url, headers=self.headers, cookies=self.cookies, callback=self.parse)#七、接收responsedef parse(self,response):pass

‘’’

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/278749.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278749.shtml
英文地址,請注明出處:http://en.pswp.cn/news/278749.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Android 電量優化

Android系統上App的電量消耗主要由cpu、wakelock、數據傳輸(流量和wifi)、wifi運行、gps、other senior組成,而耗電異常也是由于這幾個模塊的使用不當。 BroaddcastReceiver 為了減少應用損耗的電量,代碼中需要盡量避免無用的操作代碼的執行 …

如何下載手機的App Store中不再存在的應用程序

Smartphone app stores are well established at this point, and as much as we love to see new apps become available, that also means the inevitable: sometimes apps go away. Here’s what you can do if your favorites disappear. 在這一點上,智能手機應…

Q_learning簡介與實例

1、算法思想 QLearning是強化學習算法中value-based的算法,Q即為在某一環境下,Q(state,action)在某一時刻的 s 狀態下(s∈S),采取 動作a (a∈A)動作能夠獲得收益的期望,環境會根據agent的動作反饋相應的回…

吳穎二:12.27 午評 地緣政治一波未平一波又起,千三可到?

前言:圣誕節后首個交易日,金銀油均走出了大行情。美國因導彈項目制裁朝鮮兩名官員,地緣局勢再升溫黃金本周能否突破1300關口?油價刷新兩年半高位后,一個重要指標卻已暗示短期內或面臨風險…… 朝鮮局勢進一步惡化的同時…

2-1 gradle安裝

因為Gradle是基于JVM的,所以一定要確保本機已經安裝了JDK,我們可以通過java -version來驗證一下是否已經安裝了JDK。 bin目錄里面是兩個可執行文件,一個是Windows下面的可執行文件,還有一個就是類Unix文件系統的可執行文件。所有的…

Django中session和cookie簡單的使用

一、簡單的理解 session和cookie是request下的兩個對象,操作他們的值就是在操作字典,設置他們的屬性就是調用方法。 會話(Session)跟蹤是Web程序中常用的技術,用來跟蹤用戶的整個會話。Web應用程序是使用HTTP協議傳輸…

攝影中的曝光補償是什么?

When you use your camera in some automatic modes like Program—or one of the semi-manual modes like Aperture Priority or Shutter Speed Priority—you don’t give up total control over everything: you can still control the exposure using exposure compensatio…

ajax回調打開新窗體防止瀏覽器攔截方法

2019獨角獸企業重金招聘Python工程師標準>>> 問題剖析: function click_fun(){ window.open("www.baidu.com");//能打開 $.ajax({ url: ${pageContext.request.contextPath}/activity/savePrizes.htm, type: post, dataType: json, data: data…

ES6學習--對象屬性的遍歷

ES6一共有5種方法可以遍歷對象的屬性。 (1)for...in for...in循環遍歷對象自身的和繼承的可枚舉屬性(不含Symbol屬性)。 (2)Object.keys(obj) Object.keys返回一個數組,包括對象自身的&#xff…

多點認證wi-fi_準備使用Wi-Fi 6:認證將于2019年第三季度啟動

多點認證wi-fiThe Wi-Fi Alliance already announced Wi-Fi 6 back in October. Today, it’s announcing the details of the Wi-Fi 6 certification process, which will launch in the third quarter of 2019. Expect many new Wi-Fi 6 devices later this year. Wi-Fi聯盟已…

jQuery初識和常用事件(一)

文章目錄一、jQuery二、入口函數三、選擇器選擇器小結 ★全部選擇器參考 ☆四、常用的 jQuery 事件方法事件寫法鼠標事件元素事件鍵盤事件文檔/窗口事件全部事件方法參考 ☆五、效果事件顯示與隱藏:hide,show,toggle淡入和淡出:fa…

nginx內嵌變量

FORWARD:http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_core_module.html#variables ngx_http_core_module模塊支持內嵌變量,變量名與Apache服務器對應。 首先,這些變量可以表示客戶端的請求頭字段,諸如$http_user_agent、$ht…

pdf文檔遇到了共享沖突_如何將鏈接共享為PDF格式的Google文檔鏈接

pdf文檔遇到了共享沖突Using Google Docs is a great way to collaborate on and share documents. Sometimes, though, you want to provide somebody with a PDF instead of an editable document. Google Docs now lets you edit your sharing link to provide a PDF. Best …

Visual Studio 2019 preview中體驗C# 8.0新語法

準備工作: Visual Studio 2019 Preview版本中并沒有包含所有的C# 8.0的新功能,但目前也有一些可以試用了。在開始之前,需要進行入兩項設置: 將Framework設置為.net core 3.0 將C#語法設置為8.0 也可以直接編輯.csproj文件&#x…

jQuery 對HTML的操作(二)

文章目錄一、jQuery獲取、設置HTML標簽的內容和屬性獲得內容 - text()、html() 以及 val()獲取屬性 - attr(),prop()二、增刪 HTML 的內容增加刪除三、操作CSSaddClass 添加removeClass 刪除toggleClass 切換css 獲取與設置所有操作html、css方法參考 ☆四、操作元素…

roku能不能安裝軟件_如何在Roku中使用Google Assistant

roku能不能安裝軟件As more of our devices connect to each other, it’s always nice to know that different products from different companies work together. A Chromecast isn’t expensive, but being able to use your TV directly with Google Assistant is better.…

在線建立或重做mysql主從復制架構方法(傳統模式和GTID模式)【轉】

mysql主從復制架構,是mysql數據庫主要特色之一,絕大多數公司都有用到。 而GTID模式是基于事務的復制模式的意思,發展到現在也是越來越多人用。 以前很多文章,介紹搭建mysql主從復制架構,是需要停止應用服務來做的,對于生產環境&am…

面試學習資料

1 Java面試資料總結 (github推薦指數高) 2 后端架構師 1. 綜合 《Java 面試題 —— 老梁 Java 面試通關要點匯總集》《Java 面試題 —— 老徐 Java 初中級準備的面試題》《Java 面試題 —— 零度 Java 面試題系列》《Java 面試題 —— 東黃的學習方法和面…

jQuery對Ajax的封裝應用(三)

文章目錄一、Ajax二、load三、ajax(參數),get,set$.ajax$.ajax的get、post簡寫形式四、ajax全部方法參考 ☆一、Ajax AJAX 是與服務器交換數據的技術,它在不重載全部頁面的情況下,實現了對部分網頁的更新。…

如何使用卡巴斯基急救盤清理感染的PC

When you’re dealing with a PC that is completely infected in viruses, sometimes the best thing to do is reboot into a rescue disk and run a full virus scan from there. Here’s how to use the Kaspersky Rescue Disk to clean an infected PC. 當您要處理一臺完全…