python爬取w3cschool python练习实例100例

w3cschool python练习实例100例

python爬取w3cschool python练习实例100例_第1张图片

代码如下:

# -*- coding: utf-8 -*-

import requests
from bs4 import BeautifulSoup

#content > p:nth-child(3) > strong
for page in range(1,101,1):
    url = "http://www.w3cschool.cn/python/python-exercise-example{}.html".format(page)

    data = requests.get(url).text
    soup = BeautifulSoup(data,"lxml")

    titles = soup.find_all(id="content")
    f = open("test1.txt",'a')
    for n in titles:
        title = n.get_text()
        print(title,file =f)

    f.close()

你可能感兴趣的:(python爬虫)