[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)

最后爬取的结果:
在这里插入图片描述

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