鱼C论坛

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

[已解决]去掉转义字符

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

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

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

x
我回来了~
问题也来拜访我了
请问这里的转义字符如何去掉?
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 = [content6[1]]
    #打印
    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 = [content6[1]]
    #打印
    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 = [content6[1]]
    #打印
    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)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 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 = [content6[1]]
    #打印
    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 = [content6[1]]
    #打印
    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)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-13 09:12:54 From FishC Mobile | 显示全部楼层
简单点用replace替换就可以了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-1-15 20:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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