顽空 2021-04-20 22:35 采纳率: 50%
浏览 82
已采纳

最近学习爬虫,可是代码出来之后效果很不对劲,有大神看看出了什么问题,谢谢了……

import requests
from bs4 import BeautifulSoup

url = 'https://699pic.com/qingnianshenghuo.html'

resp = requests.get(url)
resp.encoding='utf-8'

main_page= BeautifulSoup(resp.text, 'html.parser')

alist = main_page.find_all("div", class_="photo-tag")
child_href_list=[]
for a in alist:


    w=a.find("a")

    hrefs = "https:"+w.get("href")
    child_href_list.append(hrefs)
    
    for href in child_href_list:
        child_page_resp = requests.get(href)
        child_page_resp.encoding="utf_8"
        child_page_text = child_page_resp.text
        child_page=BeautifulSoup(child_page_text,"html.parser")
        p = child_page.find("a", class_="photo-img-link")

        img = p.find("img")



        print("https:"+img.get("src"))

主要是抓取的图片重复的太厉害,尤其是第一张图片,没有规律的循环重复……

  • 写回答

4条回答 默认 最新

  • CSDN专家-HGJ 2021-04-20 22:51
    关注
    for href in child_href_list:
            child_page_resp = requests.get(href)
            child_page_resp.encoding="utf_8"
            child_page_text = child_page_resp.text
            child_page=BeautifulSoup(child_page_text,"html.parser")
            p = child_page.find("a", class_="photo-img-link")
    
            img = p.find("img")
    
    
    
            print("https:"+img.get("src"))

    这段代码不要放到for a in alist:循环里,提到外面就可以了

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

报告相同问题?

悬赏问题

  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?