# 导入所需的库
import requests
from bs4 import BeautifulSoup
import re
import time
for i in range(1, 13):
url = 'http://www.cntour.cn/'
html = requests.get(url) # requests.get获得网页的HTML
soup = BeautifulSoup(html.text, 'lxml')
print('正在打印第%d条' %i)
data = soup.select('#main > div > div.mtop.firstMod.clearfix > div.leftBox > div:nth-of-type(2) > ul > '
'li:nth-of-type({0}) > a'.format(i))
for item in data: # 简单的for循环遍历data中的数据
result={
'title':item.get_text(),
'link':item.get('href'),
'ID':re.findall('\d+', item.get('href'))
}
time.sleep(1) # 设定休眠时间,每隔1秒打印一次
print(result)