html 去除转义字符,怎么去除爬下来的网站中的一些转义字符串

08907253edf20ee5978246684ad1b64b.png

from bs4 import BeautifulSoup

import re

import urllib.request, urllib.error

def first(baseurl):

return getData(baseurl)

findLink = re.compile(r'', re.S)

findTttle = re.compile(r'(.?)', re.S)

findContant1 = re.compile(r'

(.)

', re.S)

findContant2 = re.compile(r'justify;">(.)

', re.S)

dr=re.compile(r']+>', re.S)

def getData(baseurl):

datalist = []

url = baseurl

html = askUrl(url)

soup = BeautifulSoup(html, "html.parser")

for presscolumn in soup.find_all('div', class_="presscolumn"):

item = str(presscolumn)

link = re.findall(findLink, item)

datalist.append(link[0])

return datalist

def askUrl(url):

head = {

"User-Agent": "Mozilla/5.0(Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"

}

request = urllib.request.Request(url, headers=head)

html = ""

try:

response = urllib.request.urlopen(request)

html = response.read().decode("utf-8")

except urllib.error.URLError as e:

if hasattr(e, "code"):

print(e.code)

if hasattr(e, "reason"):

print(e.reason)

return html

def getData2(baseurl2):

datalist2 = []

url = baseurl2

html = askUrl(url)

soup = BeautifulSoup(html, "html.parser")

for overall in soup.find_all('div', class_="overall"):

item = str(overall)

title = re.findall(findTttle, item)[0]

datalist2.append(title)

contant =re.findall(findContant1, item)

if len(contant) <= 0:

contant = re.findall(findContant2, item)

contant = dr.sub('', str(contant))

datalist2.append(contant)

return datalist2

def main():

baseurl = input("请输入您要的网站:")

data = []

baseurl2 = first(baseurl)

for n in range(0, len(baseurl2)):

print(getData2(baseurl2[n]))

data.append(getData2(baseurl2[n]))

if name == "main":

main()

兄弟萌,这些\和后面的东西怎么去掉啊

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值