如您所见,我定义了两个变量:一个名为 href
的变量,它有多个链接作为一个字符串;另一个名为 text
的变量,现在位于 text 中
我有我已经访问过/下载过的链接。我希望 Python 打印 href
中存在的文本,但 text
中不存在的文本。
所以我想象它使用 for 循环?
当我执行单个字母时,会返回单个字母,所有字母都在不同的行上分隔。
import requests
from bs4 import BeautifulSoup
url = 'amazon.com'
source_code = requests.get(url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text, 'html.parser')
for link in soup.findAll('a', {'class': 'gridItem-trackInfo-title-anchor'}):
href = link.get('href')
file = open('file.txt', 'r')
text = file.read()
file.close
for i in href:
if i not in text:
print(i)
最佳答案
如果您只想在一行上输入,请使用 print(i, end='')
就可以了。
如果你想要链接,你应该这样做
for i in links(href):
if i not in links(text):
print(i)
可以在retrieve links from web page using python and BeautifulSoup找到links
功能。
如果您想要链接而不是字母,请使用:
if link not in text:
print(link)
在循环每个链接的字母之前。
而不是:
for i in href:
if i not in text:
print(i)
关于python - 查找字符串中不存在的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34384192/