[python3]糗事百科爬蟲-正則表達式【2】

[python3]糗事百科爬蟲-正則表達式【2】


上一篇主要是採用的find()來定位糗事百科的作者、頭像、性別、年齡、段子,感覺有點low。
這次我們採用正則表達式來定位
上方法

在這裏插入圖片描述
比如:針對段子內容span
req_pattern = re.compile(
‘<div class="article block untagged mb15 typs.?>.?class=“content”.?span>(.?)’,
re.S)

部分內碼如下:

for pageIndex in range(1,14): #按照ID頁數來依次爬取不同頁的內容
    url = 'https://www.qiushibaike.com/hot/page/' + str(pageIndex)
    #print(url)
    response = requests.get(url, headers=head)
    html=response.text
    #以下是段子內容
    req_pattern = re.compile(
        '<div class="article block untagged mb15 typs.*?>.*?class="content".*?span>(.*?)</span>',
        re.S)
    info = re.findall(req_pattern, html)
    #print(info)
    #以下是作者名稱
    req_pattern1 = re.compile(
        '<div class="article block untagged mb15 typs.*?>.*?class="author clearfix".*?h2>(.*?)</h2>',
        re.S)
    author=re.findall(req_pattern1, html)
    #print(author)
    # 以下是作者頭像
    req_pattern2=re.compile(
        '<div class="article block untagged mb15 typs.*?>.*?class="author clearfix".*?<img src="(.*?)" alt',
        re.S)
    img = re.findall(req_pattern2, html)
    # print(img)

    # 以下是作者性別
    req_pattern3 = re.compile(
        '<div class="article block untagged mb15 typs.*?>.*?class="author clearfix".*?class="articleGender (.*?)Icon"',
        re.S)
    gender = re.findall(req_pattern3, html)
    #print(gender)
    #以下是作者年齡
    req_pattern4 = re.compile(
        '<div class="article block untagged mb15 typs.*?>.*?class="articleGender.*?Icon">(.*?)</div>',
        re.S)
    age = re.findall(req_pattern4, html)
    #print(age)

最後爬取的結果:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章