Python3 黑板客爬虫闯关第一关

#coding=utf-8
import re
import requests
from requests.exceptions import RequestException
from bs4 import BeautifulSoup

def getHtml(url):
    try:
        headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0'}  
        response = requests.get(url,headers = headers)
        if response.status_code == 200:
            return response.text
        return None
    except RequestException:
        return None
 
if __name__=='__main__':
    start_url = "http://www.heibanke.com/lesson/crawler_ex00/"
    real_url = start_url
    while 1:#while 1 的运行速度比while True 要快那么一点
        print ("当前请求页面:{}".format(real_url))
        html = getHtml(real_url)
        soup = BeautifulSoup(html,"lxml")
        source =soup.select_one('h3').text
        num = re.findall('\d+',source)
        if len(num) == 0:
            break
        real_url =  start_url + num[0]

考察点:url的拼接,BeautifulSoup库及正则表达式库的使用。

猜你喜欢

转载自blog.csdn.net/qq523176585/article/details/83019159