抓取某一用戶微博

任務(wù)目標(biāo)

1. 選取任意用戶的微博(首頁)
2. 抓取信息,包括: 用戶名稱、用戶簡介、前10條微博信息,有圖片的,包含圖片地址

目標(biāo)地址

http://weibo.com/pinganbeijing

模擬登陸方式

1. 登錄模擬表單登錄 (驗(yàn)證碼識別)
2. Cookie登錄

抓取用戶名稱和用戶簡介

需要抓取的信息

用戶名稱

用戶簡介

安裝Selenium

pip install Selenium

安裝phantomjs

http://phantomjs.org/download.html

將安裝后的EXE文件放到C:\Python27\Scripts文件夾下。

使用XPath

XPath的簡單調(diào)用方法
from lxml import etree
selector=etree.HTML(源碼)  #將源碼轉(zhuǎn)化為能被XPath匹配的格式
selector.xpath(表達(dá)式)  #返回為一列表
XPath的簡單調(diào)用方法
1) // 雙斜杠 定位根節(jié)點(diǎn),會對全文進(jìn)行掃描,在文檔中選取所有符合條件的內(nèi)容,以列表的形式返回。 
2) / 單斜杠 尋找當(dāng)前標(biāo)簽路徑的下一層路徑標(biāo)簽或者對當(dāng)前路標(biāo)簽內(nèi)容進(jìn)行操作 
3) /text() 獲取當(dāng)前路徑下的文本內(nèi)容 
4) /@xxxx 提取當(dāng)前路徑下標(biāo)簽的屬性值 
5) | 可選符 使用|可選取若干個路徑 如//p | //div 即在當(dāng)前路徑下選取所有符合條件的p標(biāo)簽和div標(biāo)簽。 
6) . 點(diǎn) 用來選取當(dāng)前節(jié)點(diǎn) 
7) .. 雙點(diǎn) 選取當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn) 

固定coolie登陸

獲取User-Agent和Cookie
# -*- coding: utf-8 -*-
from lxml import etree
from selenium import webdriver

class ZirconSinaSpider:

    driver = webdriver.PhantomJS()  # 使用webdriver.PhantomJS

    def get_url_content(self, url):
        self.init_phantom_driver(url)

    def init_phantom_driver(self, url):
        cap = webdriver.DesiredCapabilities.PHANTOMJS
        cap["phantomjs.page.settings.resourceTimeout"] = 1000

        user_agent = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3100.0 Safari/537.36'
        cookie = 'your_cookie'
        headers = {
            'User-Agent': user_agent,
            'Cookie': cookie
        }

        for key, value in headers.iteritems():
            cap['phantomjs.page.customHeaders.{}'.format(key)] = value
            cap['phantomjs.page.customHeaders.User-Agent'] = user_agent
            self.driver = webdriver.PhantomJS(desired_capabilities=cap)

        self.driver.get(url)
        doctree = self.get_dom_tree()
        self.get_content_username(doctree)
        self.get_content_user_sim_info(doctree)
        self.get_content_top10_info(doctree)

    def get_dom_tree(self):
        # 執(zhí)行js得到整個dom
        html = self.driver.execute_script("return document.documentElement.outerHTML")
        doctree = etree.HTML(html)
        print type(doctree)
        return doctree

    def get_content_username(self,doctree):
        username = doctree.xpath('//*[@id="Pl_Official_Headerv6__1"]/div/div/div[2]/div[2]/h1/text()')  # 獲取用戶名稱, 這里使用id屬性來定位哪個div被匹配 使用text()獲取文本內(nèi)容
        print username[0]

    def get_content_user_sim_info(self,doctree):
        user_sim_info = doctree.xpath('//*[@id="Pl_Official_Headerv6__1"]/div/div/div[2]/div[3]/@title')  # 獲取用戶簡介, 使用“@標(biāo)簽屬性”獲取div便簽的title屬性值
        print user_sim_info[0]

    def get_content_top10_info(self,doctree):
        user_top10_info = doctree.xpath('//div[@class="WB_detail"]/div[@class="WB_text W_f14"]')  # 獲取用戶前10條微博
        i = 1
        for each in user_top10_info:
            print i
            i = i + 1
            print each.xpath('string(.)').strip()

url = "http://weibo.com/pinganbeijing"
spider = ZirconSinaSpider()
spider.get_url_content(url)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容