标签:www none 练习 mamicode for 导入 splay 爬虫基础 slist
爬虫基础练习——抓取网页数据
题目:抓取http://www.cntour.cn/首页新闻
分析:依次找到要抓取的数据的节点
使用筛选器依次找到要抓取的节点
#main>div>div.mtop.firstMod.clearfix>div.centerBox>ul.newsList>li>a
然后代码如下:
import requests #导入requests包
import re
from bs4 import BeautifulSoup
url=‘http://www.cntour.cn/‘
strhtml=requests.get(url)
soup=BeautifulSoup(strhtml.text,‘lxml‘)
data = soup.select(‘#main>div>div.mtop.firstMod.clearfix>div.centerBox>ul.newsList>li>a‘)
for item in data:
result={
‘ID‘:re.findall(‘\d+‘,item.get(‘href‘)),
‘title‘:item.get_text(),
‘link‘:item.get(‘href‘)
}
print(result)
结果如下:
标签:www none 练习 mamicode for 导入 splay 爬虫基础 slist
原文地址:https://www.cnblogs.com/madyina/p/12257503.html