结果咧 发表于 2020-8-2 17:26:43

爬取100道python题

正则表达式:
import re
import requests

def gethtml(url):
    try:
      r=requests.get(url)
      r.raise_for_status()
      r.encoding=r.apparent_encoding
      return r.text
    except:
      print("处理异常")

def extrinfo(txt,i):
    try:
      r=re.findall(r"</strong>.+?</p>",txt)
      r=r.replace("</strong>","")
      r=r.replace("</p>","")
      print(str(i)+":\t"+r)
    except:
      print(str(i)+"\t"+"处理异常")

def main(i):
    url="https://www.runoob.com/python/python-exercise-example"+str(i)+".html"
    extrinfo(gethtml(url),i)

if __name__ == "__main__":
    for i in range(1,101):
      main(i)

效果如图:

页: [1]
查看完整版本: 爬取100道python题