Python爬虫遇到的问题(二)---关于beautifulsoup select方法时得到空列表的问题

问题

右键点击审查,然后在弹出的html源码中右键选择Copy–>Copy selector
得到

#topic > dl:nth-child(3) > div > div.newsbottom > ul > li:nth-child(8) > a

描述了我们想要获取的内容在html中的由外层到内层的位置/路径信息。

from bs4 import BeautifulSoup
import url_get
import urllib.request

def get_html(url):

    page = urllib.request.urlopen(url) # 打开网页

    htmlcode = page.read().decode("gbk") # 读取页面源码 ‘gbk’解决中文乱码问题,不用utf-8因为utf-8报错,可能是因为特殊字符不支持

    return htmlcode

url = 'http://www.zjgsu.edu.cn/news/' #浙江工商大学新闻网

html = url_get.get_html(url) #获取html

soup = BeautifulSoup(html,'html.parser')  #定义一个Soup对象

#topic > dl:nth-child(3) > div > div.newsbottom > ul > li:nth-child(8) > a
#Copy selector得到的
newses = soup.select('topic > dl:nth-of-type(3) > div > div.newsbottom > ul > li:nth-of-type(8) > a')
for news in newses:
    print(new
  • 10
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值