附加到列表后,在html源代码中删除\ n

我正在尝试实现对网站的获取请求,获取html并将其附加到列表中。问题是它在随机的位置添加了\n,我需要编写一个脚本来解决该问题。我已经尝试过strip()replace()及其之间的所有操作。

这是我的代码:

r = requests.get(page)
data = r.text
html = BeautifulSoup(data,"html.parser")

for lin in html.find_all("link",href=True):
    if "css" in lin['href']:
        urls.append(lin['href'])

for url in urls:
    if "http" in url:
        sourcecode.append(data)

我只需要从源代码中删除\n

hyswsh 回答:附加到列表后,在html源代码中删除\ n

我希望能解决您的问题。我在某些页面上对其进行了检查,并且可以正常工作。

r = requests.get(page)
data = r.text
html = BeautifulSoup(data,"html.parser")

for lin in html.find_all("link",href=True):
    if "css" in lin['href']:
        urls.append(lin['href'].replace("\n",""))

for url in urls:
    if "http" in url:
        sourcecode.append(data)
,
urls.append(lin['href'].replace("\n",""))
,

我通过以二进制模式打开文件解决了这个问题!

f =打开(“文件”,“ ab +”)

本文链接:https://www.f2er.com/3084338.html

大家都在问