Python爬蟲入門-2

  • robots.txt :
  • UA:
  • 1.指定url
  • 2.發起請求
  • 3.獲取頁面數據
  • 4.數據解析
  • 5.持久化存儲html

  • bs4:dom

    • 實例化bs對象,將頁面源碼數據加載到該對象中
    • 定位標籤:find('name',class_='xxx') findall() select()
    • 將標籤中的文本內容獲取 string text get_text() a['href']
 
  • 環境安裝:pip install lxml
  • 解析原理:
    • 獲取頁面源碼數據
    • 實例化一個etree的對象,而且將頁面源碼數據加載到該對象中
    • 調用該對象的xpath方法進行指定標籤的定位
    • 注意:xpath函數必須結合着xpath表達式進行標籤訂位和內容捕獲
/html/head/title
//head/title
//title
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>測試bs4</title>
</head>
<body>
    <div>
        <p>百里守約</p>
    </div>
    <div class="song">
        <p>李清照</p>
        <p>王安石</p>
        <p>蘇軾</p>
        <p>柳宗元</p>
        <a href="http://www.song.com/" title="趙匡胤" target="_self">
            <span>this is span</span>
        宋朝是最強大的王朝,不是軍隊的強大,而是經濟很強大,國民都頗有錢</a>
        <a href="" class="du">總爲浮雲能蔽日,長安不見令人愁</a>
        <img src="http://www.baidu.com/meinv.jpg" alt="" />
    </div>
    <div class="tang">
        <ul>
            <li><a href="http://www.baidu.com" title="qing">清明時節雨紛紛,路上行人慾斷魂,借問酒家何處有,牧童遙指杏花村</a></li>
            <li><a href="http://www.163.com" title="qin">秦時明月漢時關,萬里長征人未還,但使龍城飛將在,不教胡馬度陰山</a></li>
            <li><a href="http://www.126.com" alt="qi">岐王宅裏尋常見,崔九堂前幾度聞,正是江南好風景,落花時節又逢君</a></li>
            <li><a href="http://www.sina.com" class="du">杜甫</a></li>
            <li><a href="http://www.dudu.com" class="du">杜牧</a></li>
            <li><b>杜小月</b></li>
            <li><i>度蜜月</i></li>
            <li><a href="http://www.haha.com" id="feng">鳳凰臺上鳳凰遊,鳳去臺空江自流,吳宮花草埋幽徑,晉代衣冠成古丘</a></li>
        </ul>
    </div>
</body>
</html>
#項目需求:解析58二手房的相關數據
import requests
from lxml import etree

url = 'https://bj.58.com/shahe/ershoufang/?utm_source=market&spm=u-2d2yxv86y3v43nkddh1.BDPCPZ_BT&PGTID=0d30000c-0047-e4e6-f587-683307ca570e&ClickID=1'
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'
}
page_text = requests.get(url=url,headers=headers).text

tree = etree.HTML(page_text)
li_list = tree.xpath('//ul[@class="house-list-wrap"]/li')
fp = open('58.csv','w',encoding='utf-8')
for li in li_list:
    title = li.xpath('./div[2]/h2/a/text()')[0]
    price = li.xpath('./div[3]//text()')
    price = ''.join(price)
    fp.write(title+":"+price+'\n')
fp.close()
print('over')
# ctrl+shift+x
# - 解析圖片數據:http://pic.netbian.com/4kmeinv/
import requests
from lxml import etree
import os
import urllib

url = 'http://pic.netbian.com/4kmeinv/'
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'
}
response = requests.get(url=url,headers=headers)
#response.encoding = 'utf-8'
if not os.path.exists('./imgs'):
    os.mkdir('./imgs')
page_text = response.text

tree = etree.HTML(page_text)
li_list = tree.xpath('//div[@class="slist"]/ul/li')
for li in li_list:
    img_name = li.xpath('./a/b/text()')[0]
    #處理中文亂碼
    img_name = img_name.encode('iso-8859-1').decode('gbk')
    img_url = 'http://pic.netbian.com'+li.xpath('./a/img/@src')[0]
    img_path = './imgs/'+img_name+'.jpg'
    urllib.request.urlretrieve(url=img_url,filename=img_path)
    print(img_path,'下載成功!')
print('over!!!')
#【重點】下載煎蛋網中的圖片數據:http://jandan.net/ooxx
#數據加密  (反爬機制)
import requests
from lxml import etree
import base64
import urllib


headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'
}
url = 'http://jandan.net/ooxx'
page_text = requests.get(url=url,headers=headers).text

tree = etree.HTML(page_text)
img_hash_list = tree.xpath('//span[@class="img-hash"]/text()')
for img_hash in img_hash_list:
    img_url = 'http:'+base64.b64decode(img_hash).decode()
    img_name = img_url.split('/')[-1]
    urllib.request.urlretrieve(url=img_url,filename=img_name)
import random
#爬取站長素材中的簡歷模板
import requests
import random
from lxml import etree
headers = {
    'Connection':'close', #當請求成功後,立刻斷開該次請求(及時釋放請求池中的資源)
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'
}
url = 'http://sc.chinaz.com/jianli/free_%d.html'
for page in range(1,4):
    if page == 1:
        new_url = 'http://sc.chinaz.com/jianli/free.html'
    else:
        new_url = format(url%page)
    
    response = requests.get(url=new_url,headers=headers)
    response.encoding = 'utf-8'
    page_text = response.text

    tree = etree.HTML(page_text)
    div_list = tree.xpath('//div[@id="container"]/div')
    for div in div_list:
        detail_url = div.xpath('./a/@href')[0]
        name = div.xpath('./a/img/@alt')[0]

        detail_page = requests.get(url=detail_url,headers=headers).text
        tree = etree.HTML(detail_page)
        download_list  = tree.xpath('//div[@class="clearfix mt20 downlist"]/ul/li/a/@href')
        download_url = random.choice(download_list)
        data = requests.get(url=download_url,headers=headers).content
        fileName = name+'.rar'
        with open(fileName,'wb') as fp:
            fp.write(data)
            print(fileName,'下載成功')
#解析全部的城市名稱
import requests
from lxml import etree
headers = {
    'Connection':'close', #當請求成功後,立刻斷開該次請求(及時釋放請求池中的資源)
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'
}
url = 'https://www.aqistudy.cn/historydata/'
page_text = requests.get(url=url,headers=headers).text

tree = etree.HTML(page_text)
li_list = tree.xpath('//div[@class="bottom"]/ul/li |  //div[@class="bottom"]/ul/div[2]/li')
for li in li_list:
    city_name = li.xpath('./a/text()')[0]
    print(city_name)
#設置請求的代理ip: www.goubanjia.com  快代理  西祠代理
#代理ip的類型必須和請求url的協議頭保持一致
url = 'https://www.baidu.com/s?wd=ip'

page_text = requests.get(url=url,headers=headers,proxies={'https':'61.7.170.240:8080'}).text

with open('./ip.html','w',encoding='utf-8') as fp:
    fp.write(page_text)
  • robots
  • UA
  • 數據加密
  • 懶加載
  • 代理ip
相關文章
相關標籤/搜索