weixin_50790233 2022-09-05 23:42 采纳率: 33.3%
浏览 10
已结题

爬虫关于获取标签内容的问题

img

代码

html=html=urlopen('https://www.qidian.com/rank/yuepiao/year2022-month08/')
obj=BeautifulSoup(html,'lxml')
for i in obj.find('ul',{'class':'list_type_detective'}).find_all('a',href=re.compile('^(www)')):
print(i)

问题
这种find().findall()的形式是不行吗,为什么爬取不到a标签

  • 写回答

2条回答 默认 最新

  • 梦里逆天 2022-09-06 00:23
    关注
    
    import re
    from urllib.request import urlopen
    
    from bs4 import BeautifulSoup
    
    html = urlopen('https://www.qidian.com/rank/yuepiao/year2022-month08/')
    # print(html)
    obj = BeautifulSoup(html, 'lxml')
    # print(obj)
    for i in obj.find(attrs={'class': 'list_type_detective'}):  # 提取class为list_type_detective的元素
        print(i)
    print('*'*8)
    for i in obj.select('ul.list_type_detective > li > a'):  # 提取class为list_type_detective的ul里的li中的a标签
        print(i['href'])
    print('-'*8)
    for i in obj.find_all('a', href=re.compile('^//www')):  # 提取以”//www"开头的a标签
        print(i)
    

    可参考:https://baijiahao.baidu.com/s?id=1703234898700447231&wfr=spider&for=pc

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月14日
  • 已采纳回答 9月6日
  • 创建了问题 9月5日

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答