槿萱 2021-02-05 11:32 采纳率: 0%
浏览 1213

爬虫爬出来的列表是空的

import requests
import re
if __name__=='__main__':
    headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36 Edg/88.0.705.56'}
    url='https://www.qiushibaike.com/imgrank/'
    #使用通用爬虫对URL对应的一整张页面进行爬取
    page_text=requests.get(url=url,headers=headers).text
    #使用聚焦爬虫对页面中所有图片进行解析、提取
    ex='<div class="thumb"><a href=".*?"target="_blank"><img src=".*?"alt=".*?"class="illustration" width="100%" height="auto"></a></div>'
    img_src_list=re.findall(ex,page_text,re.S)
    print(img_src_list)

D:\phython\anaconda\python.exe D:/phython/pycham/练习/爬虫练习/聚焦爬虫.py
[]

Process finished with exit code 0

  • 写回答

1条回答 默认 最新

  • coagenth 2021-02-05 11:48
    关注

    不建议用re进行页面解析,用bs4库解析,代码如下:

    import requests

    from bs4 import BeautifulSoup as bs

    #import re

    if __name__=='__main__':

        headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36 Edg/88.0.705.56'}

        url='https://www.qiushibaike.com/imgrank/'

        #使用通用爬虫对URL对应的一整张页面进行爬取

        page_text=requests.get(url=url,headers=headers).text

        #使用聚焦爬虫对页面中所有图片进行解析、提取

        soup=bs(page_text,'lxml')

        img_src=soup.select('div.thumb img')

        img_list=[]

        for tag in img_src:

            img_list.append("https:"+tag['src']+","+tag['alt'])

        print(img_list)

    输出图片链接地址和标签名

    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大