鱼C论坛

 找回密码
 立即注册
查看: 2586|回复: 17

[已解决]关于正则匹配

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

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

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

x
8@W9YW5CSQ$[`YYFNJ6}DT7.png
a=re.compile(r'<img style=".*?src="(.*?[.]jpg)"',re.S)
我这样直接在html里面匹配为什么什么都没有匹配到
最佳答案
2020-7-11 12:02:45
本帖最后由 suchocolate 于 2020-7-11 12:06 编辑

看了网页,它把图片信息放到js里了,你这样搜:
  1. def get_list(html):
  2.     link_list = re.findall(r'img":"(.*?\.jpg)', html.text)
  3.     print(link_list)
  4.     return link_list
复制代码

我这能输出:
1.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-7-11 11:01:42 | 显示全部楼层
  1. import re
  2. import requests

  3. def get_html(url):
  4.     header={
  5.         'User-Agent': 'Mozilla / 5.0(Windows NT 10.0;WOW64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 78.0.3904.108Safari / 537.36QIHU360EE'
  6.     }
  7.     html=requests.get(url=url,headers=header)
  8.     print(html.text)
  9.     return html

  10. def get_list(html):
  11.     a=re.compile(r'<img style=".*?src="(.*?[.]jpg)"',re.S)
  12.     link_list=re.findall(a,html.text)
  13.     print(link_list)
  14.     return link_list

  15. def main():
  16.     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'
  17.     html=get_html(url)

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

使用道具 举报

发表于 2020-7-11 11:02:43 | 显示全部楼层


有的时候审核元素会有一些字符不会显示,比如 \ ,你先打印下看看网页的源码,然后在对照着匹配试试看

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

使用道具 举报

 楼主| 发表于 2020-7-11 11:09:08 From FishC Mobile | 显示全部楼层
Twilight6 发表于 2020-7-11 11:02
有的时候审核元素会有一些字符不会显示,比如 \ ,你先打印下看看网页的源码,然后在对照着匹配试试看
...

是查看网页源代码还是打印网页的html?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-11 11:10:09 | 显示全部楼层
tiger吴 发表于 2020-7-11 11:09
是查看网页源代码还是打印网页的html?

都可以
最好打印 html
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-11 11:11:51 From FishC Mobile | 显示全部楼层
tiger吴 发表于 2020-7-11 11:09
是查看网页源代码还是打印网页的html?

还有Referer在哪儿找呢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-11 11:12:29 | 显示全部楼层
tiger吴 发表于 2020-7-11 11:09
是查看网页源代码还是打印网页的html?



帮你看完了,打印出来的网站源码标签和原本网站的不一致,可能是被反爬,或者是通过另外加载文件的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-11 11:12:58 | 显示全部楼层
tiger吴 发表于 2020-7-11 11:11
还有Referer在哪儿找呢

也是在F12 请求头里找的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-11 11:52:17 From FishC Mobile | 显示全部楼层
Twilight6 发表于 2020-7-11 11:02
有的时候审核元素会有一些字符不会显示,比如 \ ,你先打印下看看网页的源码,然后在对照着匹配试试看
...

如图,如果用BeautifulSoup匹配tag
是soup.findall('span',class_='img',style_='…')
style属性的值怎么写呢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-11 12:02:45 | 显示全部楼层    本楼为最佳答案   
本帖最后由 suchocolate 于 2020-7-11 12:06 编辑

看了网页,它把图片信息放到js里了,你这样搜:
  1. def get_list(html):
  2.     link_list = re.findall(r'img":"(.*?\.jpg)', html.text)
  3.     print(link_list)
  4.     return link_list
复制代码

我这能输出:
1.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-11 12:45:00 | 显示全部楼层
tiger吴 发表于 2020-7-11 11:52
如图,如果用BeautifulSoup匹配tag
是soup.findall('span',class_='img',style_='…')
style属性的值怎 ...

你是想匹配 src 嘛? 这样应该可以匹配到,只不过网站是js加载的就匹配不到了吧

  1. x = soup.findall('span',attrs={'style_':'width: 254px; height: 181px;'})
  2. for i in x:
  3.     print(i.img['src'])
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-11 14:21:14 From FishC Mobile | 显示全部楼层
suchocolate 发表于 2020-7-11 12:02
看了网页,它把图片信息放到js里了,你这样搜:

我这能输出:

放图片信息的js怎么查看呢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-11 14:28:50 | 显示全部楼层
本帖最后由 suchocolate 于 2020-7-11 14:29 编辑
tiger吴 发表于 2020-7-11 14:21
放图片信息的js怎么查看呢

把网页保存,以 <script type="text/data" 开头的那一行即是。
  1.     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'
  2.     headers = {'user-agent': 'Mozilla / 5.0(Windows NT 10.0;WOW64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 78.0.3904.108Safari / 537.36QIHU360EE'}
  3.     r = requests.get(url, headers=headers)
  4.     with open('r.txt', 'w') as f:
  5.         f.write(r.text)
复制代码
2.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-11 18:53:53 From FishC Mobile | 显示全部楼层
suchocolate 发表于 2020-7-11 14:28
把网页保存,以

图片下载好后,点开图片
Image not loaded
Try to open it externalliy to fix format problem
这是怎么情况,格式不对?开始我给图片文件后缀写的.jpg,后面改为.png,结果都是这样
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-11 19:18:10 From FishC Mobile | 显示全部楼层
Twilight6 发表于 2020-7-11 12:45
你是想匹配 src 嘛? 这样应该可以匹配到,只不过网站是js加载的就匹配不到了吧


图片下载好后,点开图片
Image not loaded
Try to open it externalliy to fix format problem
这是怎么情况,格式不对?开始我给图片文件后缀写的.jpg,后面改为.png,结果都是这样
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-11 20:27:52 | 显示全部楼层
tiger吴 发表于 2020-7-11 19:18
图片下载好后,点开图片
Image not loaded
Try to open it externalliy to fix format problem


可能是你爬取写入的有问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-12 09:57:51 | 显示全部楼层
tiger吴 发表于 2020-7-11 18:53
图片下载好后,点开图片
Image not loaded
Try to open it externalliy to fix format problem

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

使用道具 举报

 楼主| 发表于 2020-7-12 14:49:07 | 显示全部楼层
  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(new_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.     #print(img_list)

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



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

使用道具 举报

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

本版积分规则

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

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

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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