鱼C论坛

 找回密码
 立即注册
查看: 1918|回复: 13

[已解决]关于爬虫

[复制链接]
发表于 2020-7-11 20:30:51 | 显示全部楼层
本帖最后由 xiaosi4081 于 2020-7-11 20:36 编辑
  1. import re
  2. import requests
  3. from bs4 import BeautifulSoup

  4. def get_html(url):
  5.     header={
  6.         'User-Agent': 'Mozilla / 5.0(Windows NT 10.0;WOW64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 78.0.3904.108Safari / 537.36QIHU360EE',
  7.         'Referer': 'https: // www.so.com / s?src = 360chrome_newtab_search & q = % E5 % B8 % 8C % E5 % B3 % B6 % E3 % 81 % 82 % E3 % 81 % 84 % E3 % 82 % 8ASec - Fetch - Mode: navigate'
  8.     }
  9.     html=requests.get(url=url,headers=header)
  10.     #print(html.text)
  11.     return html

  12. # def get_list(html):
  13. #     html=html.text
  14. #     soup=BeautifulSoup(html,'html.parser')
  15. #     img_re=re.compile('https://p\d[.]ssl[.]qhimgs1[.]com/sdr/400__/.*?[.]jpg')
  16. #     img_list=soup.find_all(img_re,soup)
  17.     # print(img_list)
  18.     #a=re.compile(r'<img style=".*?src="(.*?[.]jpg)"',re.S)
  19.     #link_list=re.findall(a,html.text)

  20.     #return link_list
  21. def get_list(html):
  22.     soup = BeautifulSoup(html.text,'html.parser')
  23.     link_list1 = soup.find_all("img")
  24.     link_list = []
  25.     for i in link_list1:
  26.         link_list.append(img_list1["src"])
  27.     #print(link_list)
  28.     return link_list


  29. def main():
  30.     url='https://image.so.com/i?src=360pic_normal&z=1&i=0&cmg=15484592.3836743514792807400.1594087443636.3574&q=%E5%B8%8C%E5%B2%9B%E3%81%82%E3%81%84%E3%82%8A'
  31.     html=get_html(url)
  32.     img_list=get_list(html)
  33.     #print(img_list)

  34.     for img_url in img_list:
  35.         img_name='美眉/'+img_url.split('/')[-1]
  36.         print(img_name)
  37.         with open(img_name,'wb') as f:
  38.             html=get_html(img_list)
  39.             print(html.content)
  40.             f.write(html.content)



  41. if __name__ == "__main__":
  42.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-3 08:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表