鱼C论坛

 找回密码
 立即注册
查看: 1630|回复: 2

[已解决]去掉转义字符

[复制链接]
发表于 2021-5-12 20:17:14 | 显示全部楼层 |阅读模式

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

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

x
我回来了~
问题也来拜访我了
请问这里的转义字符如何去掉?
  1. import requests
  2. from bs4 import BeautifulSoup

  3. try:
  4.     #爬虫模块
  5.     response = requests.get("http://www.weather.com.cn/weather1d/101230301.shtml")
  6.     response.encoding = "utf-8"
  7.     #网页内容提取模块
  8.     bs = BeautifulSoup(response.text,'html.parser')
  9.     # print(bs.find_all('div',class_='t'))
  10.     content1 = bs.find_all('p',class_='tem');content1_=[]
  11.     content2 = bs.find_all('p',class_='wea');content2_=[]
  12.     content3 = bs.find_all('p',class_='win');content3_=[]
  13.     content4 = bs.find_all('p',class_='sun sunUp');content4_=[]
  14.     content5 = bs.find_all('p',class_='sun sunDown');content5_=[]
  15.     content6 = bs.find_all('ul',class_='clearfix');content6_=[]
  16.     content6 = [content6[1]]
  17.     #打印
  18.     for content in content1:
  19.         content1_.append(content.get_text())

  20.     for content in content2:
  21.         content2_.append(content.get_text())

  22.     for content in content3:
  23.         content3_.append(content.get_text())

  24.     for content in content4:
  25.         content4_.append(content.get_text())

  26.     for content in content5:
  27.         content5_.append(content.get_text())

  28.     for content in content6:
  29.         content6_.append(content.get_text())

  30.     print(content1_)
  31.     print(content2_)
  32.     print(content3_)
  33.     print(content4_)
  34.     print(content5_)
  35.     print(content6_)

  36. except Exception as e:
  37.     #打印异常信息
  38.     print(e)
复制代码

输出:
['\n24°C\n', '\n31°C\n']
['中雨', '小雨']
['\n\n<3级\n', '<3级']
['\n日出 05:16\n']
['\n日落 18:40\n']
['\n\n12日夜间\n\n中雨\n\n\n\n24°C\n\n\n\n<3级\n\n\n日落 18:40\n\n\n\n\n13日白天\n\n小雨\n\n天空阴沉\n\n\n天预报综合天气现象、能见度、空气质量等因子,预测未来一周的天空状况。\n\n\n天空蔚蓝\n可见透彻蓝天,或有蓝天白云美景\n\n\n天空淡蓝\n天空不够清澈,以浅蓝色为主\n\n\n天空阴沉\n阴天或有雨雪,天空灰暗\n\n\n天空灰霾\n出现霾或沙尘,天空灰蒙浑浊\n\n\n\n\n\n\n31°C\n\n<3级\n\n日出 05:16\n\n\n']
最佳答案
2021-5-13 09:05:41
  1. import requests
  2. from bs4 import BeautifulSoup

  3. try:
  4.     #爬虫模块
  5.     response = requests.get("http://www.weather.com.cn/weather1d/101230301.shtml")
  6.     response.encoding = "utf-8"
  7.     #网页内容提取模块
  8.     bs = BeautifulSoup(response.text,'html.parser')
  9.     # print(bs.find_all('div',class_='t'))
  10.     content1 = bs.find_all('p',class_='tem');content1_=[]
  11.     content2 = bs.find_all('p',class_='wea');content2_=[]
  12.     content3 = bs.find_all('p',class_='win');content3_=[]
  13.     content4 = bs.find_all('p',class_='sun sunUp');content4_=[]
  14.     content5 = bs.find_all('p',class_='sun sunDown');content5_=[]
  15.     content6 = bs.find_all('ul',class_='clearfix');content6_=[]
  16.     content6 = [content6[1]]
  17.     #打印
  18.     for index in range(1,7):
  19.         for content in eval(f'content{index}'):
  20.             print(content.get_text())


  21. except Exception as e:
  22.     #打印异常信息
  23.     print(e)
复制代码

  1. import requests
  2. from bs4 import BeautifulSoup

  3. try:
  4.     #爬虫模块
  5.     response = requests.get("http://www.weather.com.cn/weather1d/101230301.shtml")
  6.     response.encoding = "utf-8"
  7.     #网页内容提取模块
  8.     bs = BeautifulSoup(response.text,'html.parser')
  9.     # print(bs.find_all('div',class_='t'))
  10.     content1 = bs.find_all('p',class_='tem');content1_=[]
  11.     content2 = bs.find_all('p',class_='wea');content2_=[]
  12.     content3 = bs.find_all('p',class_='win');content3_=[]
  13.     content4 = bs.find_all('p',class_='sun sunUp');content4_=[]
  14.     content5 = bs.find_all('p',class_='sun sunDown');content5_=[]
  15.     content6 = bs.find_all('ul',class_='clearfix');content6_=[]
  16.     content6 = [content6[1]]
  17.     #打印
  18.     for index in range(1,7):
  19.         for content in eval(f'content{index}'):
  20.             eval(f'content{index}_').append(content.get_text().replace('\n',''))

  21.         print(eval(f'content{index}_'))


  22. except Exception as e:
  23.     #打印异常信息
  24.     print(e)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-5-13 09:05:41 | 显示全部楼层    本楼为最佳答案   
  1. import requests
  2. from bs4 import BeautifulSoup

  3. try:
  4.     #爬虫模块
  5.     response = requests.get("http://www.weather.com.cn/weather1d/101230301.shtml")
  6.     response.encoding = "utf-8"
  7.     #网页内容提取模块
  8.     bs = BeautifulSoup(response.text,'html.parser')
  9.     # print(bs.find_all('div',class_='t'))
  10.     content1 = bs.find_all('p',class_='tem');content1_=[]
  11.     content2 = bs.find_all('p',class_='wea');content2_=[]
  12.     content3 = bs.find_all('p',class_='win');content3_=[]
  13.     content4 = bs.find_all('p',class_='sun sunUp');content4_=[]
  14.     content5 = bs.find_all('p',class_='sun sunDown');content5_=[]
  15.     content6 = bs.find_all('ul',class_='clearfix');content6_=[]
  16.     content6 = [content6[1]]
  17.     #打印
  18.     for index in range(1,7):
  19.         for content in eval(f'content{index}'):
  20.             print(content.get_text())


  21. except Exception as e:
  22.     #打印异常信息
  23.     print(e)
复制代码

  1. import requests
  2. from bs4 import BeautifulSoup

  3. try:
  4.     #爬虫模块
  5.     response = requests.get("http://www.weather.com.cn/weather1d/101230301.shtml")
  6.     response.encoding = "utf-8"
  7.     #网页内容提取模块
  8.     bs = BeautifulSoup(response.text,'html.parser')
  9.     # print(bs.find_all('div',class_='t'))
  10.     content1 = bs.find_all('p',class_='tem');content1_=[]
  11.     content2 = bs.find_all('p',class_='wea');content2_=[]
  12.     content3 = bs.find_all('p',class_='win');content3_=[]
  13.     content4 = bs.find_all('p',class_='sun sunUp');content4_=[]
  14.     content5 = bs.find_all('p',class_='sun sunDown');content5_=[]
  15.     content6 = bs.find_all('ul',class_='clearfix');content6_=[]
  16.     content6 = [content6[1]]
  17.     #打印
  18.     for index in range(1,7):
  19.         for content in eval(f'content{index}'):
  20.             eval(f'content{index}_').append(content.get_text().replace('\n',''))

  21.         print(eval(f'content{index}_'))


  22. except Exception as e:
  23.     #打印异常信息
  24.     print(e)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-13 09:12:54 From FishC Mobile | 显示全部楼层
简单点用replace替换就可以了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-24 01:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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