鱼C论坛

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

[已解决]关于爬虫

[复制链接]
发表于 2020-7-11 20:25:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. img.png
复制代码
  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.     link_list = re.findall(r'img":"(.*?\.jpg)', html.text)
  23.     #print(link_list)
  24.     return link_list


  25. def main():
  26.     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'
  27.     html=get_html(url)
  28.     img_list=get_list(html)
  29.     #print(img_list)

  30.     for img_url in img_list:
  31.         img_name='美眉/'+img_url.split('/')[-1]
  32.         print(img_name)
  33.         with open(img_name,'wb') as f:
  34.             # print('开始爬取图片')
  35.             # html=get_html(img_list)
  36.             # print(html.content)
  37.             f.write(html.content)
  38.             print('爬取成功')



  39. if __name__ == "__main__":
  40.     main()
复制代码

爬虫爬取的图片打开后显示这样,什么问题?怎么解决尼
最佳答案
2020-7-11 20:36:54
本帖最后由 Twilight6 于 2020-7-12 14:48 编辑



这样就好了,你打印下你爬到的链接发现是这样的:

'http:\\/\\/f1.huatiku.com\\/attachment\\/forum\\/201808\\/20\\/111505racut3fuqpw7kskc.jpg'

只要用 re 模块的 sub 把 \ 替换成空字符即可,但是只能爬几个图片,然后就被反爬了


  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.     link_list = re.findall(r'img":"(.*?\.jpg)', html.text)
  23.     #print(link_list)
  24.     return link_list


  25. def main():
  26.     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'
  27.     html=get_html(url)
  28.     img_list=get_list(html)

  29.     for img_url in img_list:
  30.         img_url = re.sub(r'\\','',img_url)
  31.         img_name='美眉'+img_url.split('/')[-1]
  32.         # print(img_name)
  33.         with open(img_name,'wb') as f:
  34.             # print('开始爬取图片')
  35.             html=get_html(img_url)
  36.             # print(html.content)
  37.             f.write(html.content)
  38.             print('爬取成功')



  39. if __name__ == "__main__":
  40.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 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
回复 支持 反对

使用道具 举报

发表于 2020-7-11 20:36:54 | 显示全部楼层    本楼为最佳答案   
本帖最后由 Twilight6 于 2020-7-12 14:48 编辑



这样就好了,你打印下你爬到的链接发现是这样的:

'http:\\/\\/f1.huatiku.com\\/attachment\\/forum\\/201808\\/20\\/111505racut3fuqpw7kskc.jpg'

只要用 re 模块的 sub 把 \ 替换成空字符即可,但是只能爬几个图片,然后就被反爬了


  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.     link_list = re.findall(r'img":"(.*?\.jpg)', html.text)
  23.     #print(link_list)
  24.     return link_list


  25. def main():
  26.     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'
  27.     html=get_html(url)
  28.     img_list=get_list(html)

  29.     for img_url in img_list:
  30.         img_url = re.sub(r'\\','',img_url)
  31.         img_name='美眉'+img_url.split('/')[-1]
  32.         # print(img_name)
  33.         with open(img_name,'wb') as f:
  34.             # print('开始爬取图片')
  35.             html=get_html(img_url)
  36.             # print(html.content)
  37.             f.write(html.content)
  38.             print('爬取成功')



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

使用道具 举报

发表于 2020-7-11 20:40:50 | 显示全部楼层



成功爬取 4 张 然后好像就被反爬报错了....
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-11 20:47:30 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-7-11 21:31:12 | 显示全部楼层
Twilight6 发表于 2020-7-11 20:40
成功爬取 4 张 然后好像就被反爬报错了....
  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.     link_list = re.findall(r'img":"(.*?\.jpg)', html.text)
  23.     new_list=[]
  24.     for each in link_list:
  25.         each=re.sub(r'\\','',each)
  26.         new_list.append(each)
  27.     #print(link_list)
  28.     return new_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.     for img_url in img_list:
  34.         print(img_url)
  35.         img_name='美眉/'+img_url.split('/')[-1]
  36.         #print(img_name)
  37.         with open(img_name,'wb') as f:
  38.             # print('开始爬取图片')
  39.             # html=get_html(img_list)
  40.             # print(html.content)
  41.             f.write(html.content)
  42.             print('爬取成功')



  43. if __name__ == "__main__":
  44.     main()
复制代码

醉了,我把这个加在get_list函数里了,结果还是下载不了正常的图片
我看了下你的,只是放在main函数里,结果却能打印出图片了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-11 21:40:45 | 显示全部楼层
tiger吴 发表于 2020-7-11 21:31
醉了,我把这个加在get_list函数里了,结果还是下载不了正常的图片
我看了下你的,只是放在main函数里 ...


我的重点是 for 循环里面有这个:

img_url = re.sub(r'\\','',img_url)


你都没认值看我的回复,代码前面我有说明呀...

22.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-11 21:56:18 From FishC Mobile | 显示全部楼层
Twilight6 发表于 2020-7-11 21:40
我的重点是 for 循环里面有这个:



我也加了,和你加的位置不一样而已
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-11 21:57:29 | 显示全部楼层
tiger吴 发表于 2020-7-11 21:56
我也加了,和你加的位置不一样而已

这个多怕一下后台就把你请求阻断了 然后回报错被强制断开链接,可能被检测出来是爬虫的了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-11 21:59:03 | 显示全部楼层
tiger吴 发表于 2020-7-11 21:56
我也加了,和你加的位置不一样而已


你的 45 行代码 # html=get_html(img_list) 不能注释掉,因为你获取的是图片 url 要访问然后将返回的图片二进制写入
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-11 22:18:11 From FishC Mobile | 显示全部楼层
Twilight6 发表于 2020-7-11 21:59
你的 45 行代码 # html=get_html(img_list) 不能注释掉,因为你获取的是图片 url 要访问然后将返回的图 ...

这是我调试时的失误,全部弄好后还是不行
这个算了,我觉得光看网上这些视频有点不够用,还是得系统性的看些书学习下,你觉得哪本适合我这样的菜鸡看呢
Screenshot_20200711_221741.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-11 22:30:43 | 显示全部楼层
还在学习中!!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-11 22:33:56 | 显示全部楼层
tiger吴 发表于 2020-7-11 22:18
这是我调试时的失误,全部弄好后还是不行
这个算了,我觉得光看网上这些视频有点不够用,还是得系统性的 ...


书籍推荐爬虫篇:

https://www.zhihu.com/question/38801925
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-12 14:36:34 From FishC Mobile | 显示全部楼层
Twilight6 发表于 2020-7-11 22:33
书籍推荐爬虫篇:

https://www.zhihu.com/question/38801925

噢力给
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-23 09:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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