Python 利用BeautifulSoup和正则表达式 来爬取旅游网数据

import re
import requests
import time
from bs4 import BeautifulSoup

url = ‘http://www.cntour.cn/
r = requests.get(url)
print(r.encoding,len(r.text))
soup = BeautifulSoup(r.text, ‘lxml’)
data = soup.select(’#main > div > div.mtop.firstMod.clearfix > div.leftBox > div > ul > li > a’) #注意删除 (:nth-child )字符串,否则会报错
print(data)
for i in data:
print(‘item:’,i.get_text(), ’ ‘,‘href:’,i.get(‘href’), ’ ‘,‘ID:’,re.findall(’\d+’,i.get(‘href’)))

猜你喜欢

转载自blog.csdn.net/qq_40256654/article/details/83903780