爬取 CSDN 搜索后标题(简单编写) v0.2版

以后还会再改,自已查看最新版,这是第二版!
1.很粗糙.
2.改进搜索标题(相对第一版)




import requests
from bs4 import BeautifulSoup
res =requests.get('https://so.csdn.net/so/search/s.do?q=疯狂python精讲&t=&u=')
#防止中文内容乱码
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text,'html.parser')
#对网页内容进行爬取
#查看网页代码,看标题在哪个位置,本文以CSDN为例,他的标题是在class="search-list"里面
for news in soup.select('.search-list'):
    #获取文本标题
    h4 = news.select('*')[0].text

    #获取链接
    a = news.select('a')[0]['href']
    print(h4,a)

效果:爬取 CSDN 搜索后标题(简单编写) v0.2版_第1张图片

你可能感兴趣的:(Python)