python爬虫学习(一)
生活随笔
收集整理的這篇文章主要介紹了
python爬虫学习(一)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
基于python2.7
get與post:
url = "http://zzk.cnblogs.com" urllib.urlopen(url)----->get方法name = urllib.urlencode({"k":"b"}) urllib.urlopen(url+name)----->pst方法開發者工具中form表單的method選項為post,那么必須使用post方法。
urllib:
import urllib import rereponse = urllib.urlopen("https://www.baidu.com") #打開指定的網頁,返回網頁所有信息 reponse_code = reponse.getcode() #獲取狀態碼 reponse_body = reponse.read() #獲取網頁內容 #直接保存網頁地址的內容到指定的文件 save = urllib.urlretrieve("https://www.baidu.com", filename="/home/guido/python/baidu.html") images = re.findall(r"src='(.*?\.jpg)'", reponse_body) #利用正則表達式匹配數據 urllib.urlretrieve(images[0], filename="/home/guido/python/baidu_images.html")拼接鏈接格式
import urllib parament = urllib.urlencode({"t":"b", "w":"ios"}) url = ("http://zzk.cnblogs.com/s?"+parament) print(url)執行結果: http://zzk.cnblogs.com/s?t=b&w=ios
urllib2:
import urllib2 url = "http://www.phpno.com"#偽造瀏覽器請求頭 send_headers = {"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8","Accept-Encoding":"gzip, deflate, sdch","Accept-Language":"zh-CN,zh;q=0.8","Cache-Control":"max-age=0","Connection":"keep-alive","Cookie":"ASPSESSIONIDCCTRDBQT=OJNFDDEANPLCEFLECFILODNN; Hm_lvt_39dcd5bd05965dcfa70b1d2457c6dcae=1484820976,1484821014,1484821053; Hm_lpvt_39dcd5bd05965dcfa70b1d2457c6dcae=1484821053","Host":"www.nm3dp.com","Referer":"https://www.baidu.com/link?url=Q_AEn1rb05AX6miw616Tx5bIWILq5K_FpUQl_eyJ7TS&wd=&eqid=cb712bbf00052caf00000003588091e9","Upgrade-Insecure-Requests":"1","User-Agent":"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36" } req = urllib2.Request(url, headers=send_headers) #合并瀏覽器向服務器發送的內容 r = urllib2.urlopen(req) print(r.read())
?
Beautiful Soup
response = urllib.uelopen("http://www.3jy.com/")
html = response.read()
創建beautifulsoup對象:
soup = Beautifulsoup(html)
格式化輸出soup對象的內容:
print(soup.prettify())
找標簽:
soup.title soup.head soup.b soup.a找屬性:
soup.p.attrs獲取文字:
soup.p.string
css選擇器:
soup.select('title') 通過標簽名查找soup.select('.sister') 通過類名查找soup.select('#link1') 通過ID名查找soup.select(p link1) 組合查找soup.select('head>title') 直接子標簽查找soup.select('a[class='sister']') 屬性查找soup.p['class'] 獲取標簽內某個屬性的值(內容)通過索引的方式把select的返回值列表,又轉換成可以用select方法的對象,可以進一步操作
aa = soup.select('body') bb = aa[o] cc = bb.select('a[class='sister']')?
轉載于:https://www.cnblogs.com/Guido-admirers/p/6307739.html
總結
以上是生活随笔為你收集整理的python爬虫学习(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ZOJ3163【思维题】
- 下一篇: Windows下安装MySQL(解压版本