首页 > 代码库 > 小白的第一个爬虫程序(糗事百科)

小白的第一个爬虫程序(糗事百科)

import urllib.request

import re


for page in range(1, 10):
    url = "https://www.qiushibaike.com/8hr/page/"+str(page)+"/"
    headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0"}

    pattern = re.compile(<div\sclass="content">(.*?)</div>, re.S)

    request = urllib.request.Request(url, headers=headers)
    response = urllib.request.urlopen(request)
    html = response.read().decode("utf-8")

    count_list = pattern.findall(html)
    for i in count_list:
        print(i.replace("<span>", "").replace("</span>", "").replace("<br/>", ""))

 

小白的第一个爬虫程序(糗事百科)