爬虫第一节(爬取标题和链接)

具体讲解见书第二章

import requests
url="http://www.cntour.cn"
strhtml=requests.get(url)
print(strhtml.text)
import bs4
from bs4 import BeautifulSoup
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 = {
        "title": item.get_text(),
        "link": item.get("href")
        
    }
    print(result)

结果如下:
{‘title’: ‘让城市文脉融入现代生活’, ‘link’: ‘http://www.cntour.cn/news/6546/’}
{‘title’: ‘新时代中俄旅游合作呼唤新作为’, ‘link’: ‘http://www.cntour.cn/news/6540/’}
{‘title’: ‘高质量标准引领景区优质旅游新时代’, ‘link’: ‘http://www.cntour.cn/news/6535/’}
{‘title’: ‘数字文旅时代来了’, ‘link’: ‘http://www.cntour.cn/news/6530/’}
{‘title’: ‘[文明之光照亮复兴之路]’, ‘link’: ‘http://www.cntour.cn/news/6541/’}
{‘title’: ‘[游遍世园会:一条龙服务]’, ‘link’: ‘http://www.cntour.cn/news/6522/’}
{‘title’: ‘[高端旅游如何发力?]’, ‘link’: ‘http://www.cntour.cn/news/6512/’}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值