jsoup爬蟲 第一天

2021-08-14 10:36:10 字數 1640 閱讀 4255

org.jsoup

jsoup

1.9.2

document doc = jsoup.connect(url)

.header("accept", "*/*")

.header("accept-encoding", "gzip, deflate")

.header("accept-language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3")

.header("referer", "")

.header("user-agent", "mozilla/5.0 (windows nt 6.1; wow64; rv:48.0) gecko/20100101 firefox/48.0")

.timeout(5000)

.get();

//獲取id = 1 的元素的文字值。

doc.select("#1").text();

//獲取id = 1 的元素的html值。

doc.select("#1").html();

//獲取id =1 直接子類class='css1' , 然後所有子類的含有class = css2 的集合

doc.select("#id > .css1 .css2");

.....

更建議用url

去完成。

//建立請求

//預設就是get,可以採用post,大小寫都行,因為原始碼裡都touppercase了。

connection.setrequestmethod("get");

//是否允許快取,預設true。

connection.setusecaches(boolean.false);

//是否開啟輸出輸入,如果是post使用true。預設是false

//設定請求頭資訊

connection.addrequestproperty("connection", "close");

//設定連線主機超時(單位:毫秒)

connection.setconnecttimeout(8000);

//設定從主機讀取資料超時(單位:毫秒)

connection.setreadtimeout(8000);

//設定cookie

connection.addrequestproperty("cookie","你的cookies" );

//開始請求

document doc = jsoup.parse(connection.getinputstream(), "gbk", "");

//開啟您的瘋狂選擇器模式

doc.select("div.so >div ~ p:eq(10)>:checked");

//todo ---

爬蟲小試第一天

第乙個遇到的問題時編碼的問題 data u5468 u6069 u5e73 print data.encode utf 8 print data.decode utf 8 print data.decode unicode escape print u u5468 u6069 u5e73 輸出的結果...

爬蟲第一天(1)

import urllib2 發請求的模組 base url 定義url 執行urlopen方法,傳入乙個url,返回乙個response物件 response urllib2.urlopen base url,timeout 60 data 有資料傳送是post請求,沒有則是get timeout...

python爬蟲(第一天)

網易雲課堂的 丘祐瑋綜述 如何爬網頁資料 使用chorme,右鍵 檢查 pip 安裝 requests pip 安裝 beautifulsoup4 pip 安裝 jupyter 執行jupyter notebook import requests res requests.get res.encod...