鱼C论坛

 找回密码
 立即注册
查看: 1408|回复: 6

[已解决]正则表达式爬取标题时小横线变为&#8211怎么办

[复制链接]
发表于 2020-4-29 23:23:54 | 显示全部楼层 |阅读模式

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

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

x
  1. import re
  2. import requests

  3. link="http://www.santostang.com/"
  4. headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36'}

  5. r=requests.get(link,headers=headers)
  6. html=r.text

  7. comment=re.compile(r'<h1 class="post-title"><a href=.*?>(.*?)</a>',flags=re.DOTALL)
  8. title_list=comment.findall(html)
  9. for each in title_list:
  10.     print(each)
复制代码
截屏2020-04-29下午11.16.08.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-29 23:24:55 | 显示全部楼层
  1. import re
  2. import requests

  3. link="http://www.santostang.com/"
  4. headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36'}

  5. r=requests.get(link,headers=headers)
  6. html=r.text

  7. comment=re.compile(r'<h1 class="post-title"><a href=.*?>(.*?)</a>',flags=re.DOTALL)
  8. title_list=comment.findall(html)
  9. for each in title_list:
  10.     print(each.replace('&#8211;',' '))
复制代码
这样就好了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2020-4-30 13:11:21 | 显示全部楼层
  1. import re
  2. import requests

  3. link = "http://www.santostang.com/"
  4. headers = {
  5.     'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36'}

  6. r = requests.get(link, headers=headers)
  7. html = r.text

  8. comment = re.compile(r'<h1 class="post-title"><a href=.*?>(.*?)</a>', flags=re.DOTALL)
  9. title_list = comment.findall(html)
  10. for each in title_list:
  11.     print(each.replace('&#8211;', chr(8211)))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2020-4-30 14:49:21 | 显示全部楼层    本楼为最佳答案   
1.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-30 17:16:08 | 显示全部楼层

原来是要unescape解码吗,感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-30 17:18:03 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-30 17:18:35 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-18 17:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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