H.E.G. 发表于 2021-5-12 20:17:14

去掉转义字符

我回来了~{:10_256:}
问题也来拜访我了{:10_245:}
请问这里的转义字符如何去掉?
import requests
from bs4 import BeautifulSoup

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

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

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

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

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

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

    print(content1_)
    print(content2_)
    print(content3_)
    print(content4_)
    print(content5_)
    print(content6_)

except Exception as e:
    #打印异常信息
    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

import requests
from bs4 import BeautifulSoup

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


except Exception as e:
    #打印异常信息
    print(e)


import requests
from bs4 import BeautifulSoup

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

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


except Exception as e:
    #打印异常信息
    print(e)

wp231957 发表于 2021-5-13 09:12:54

简单点用replace替换就可以了
页: [1]
查看完整版本: 去掉转义字符