Python 如何编写多个链接以及如何计算它们?
在编写图像下载程序时,我正在测试一些功能,我想将所有链接存储在links.txt中,但它只写一个链接,在运行窗口中显示所有已建立的链接。请帮助我修复此问题,如果问题已修复,我想知道有多少链接,我尝试了一些功能,但它似乎没有像我想要的那样工作,非常感谢 以下是迄今为止的代码:Python 如何编写多个链接以及如何计算它们?,python,python-3.x,Python,Python 3.x,在编写图像下载程序时,我正在测试一些功能,我想将所有链接存储在links.txt中,但它只写一个链接,在运行窗口中显示所有已建立的链接。请帮助我修复此问题,如果问题已修复,我想知道有多少链接,我尝试了一些功能,但它似乎没有像我想要的那样工作,非常感谢 以下是迄今为止的代码: # import random # import urllib.request import requests from bs4 import BeautifulSoup def Download_Image_from_W
# import random
# import urllib.request
import requests
from bs4 import BeautifulSoup
def Download_Image_from_Web(url):
# name = random.randrange(1, 1000)
# fullName = str(name) + ".jpg"
source_code = requests.get(url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text, "html.parser")
for link in soup.findAll('img'):
image_links = link.get('src')
if '.jpg' in image_links:
raw_text = r'links.txt'
fw = open(raw_text, 'w')
for i in image_links.split("\\n"):
fw.write(i+'\n')
fw.close()
fr = open('links.txt', 'r')
text = fr.read()
print(text)
Download_Image_from_Web("https://pixabay.com/")
您必须在
append
模式下打开文件写入write
模式下的注释。
基本上,write
模式会覆盖文件,因此您的代码将
fw = open(raw_text, 'a') #this opens file in append mode
for i in image_links.split("\\n"):
fw.write(i+'\n')
fw.close()
fr = open('links.txt', 'r')
text = fr.read() #this prints all the written content for each line
print(text)
你可以像这样在结尾处打印全部书面内容
fw = open(raw_text, 'a') #this opens file in append mode
for i in image_links.split("\\n"):
fw.write(i+'\n')
fw.close()
fr = open('links.txt', 'r')
text = fr.read() #this prints all the written content at the end.
print(text)
如果您需要图像计数,则可以在image\u链接上使用len
方法。拆分(\\n”)
,因此在这种情况下,您的代码将
fw = open(raw_text, 'a') #this opens file in append mode
Images= image_links.split("\\n")
print "no of images = ",len(Images)
for i in Images:
fw.write(i+'\n')
fw.close()
fr = open('links.txt', 'r')
text = fr.read() #this prints all the written content at the end
print(text)
以下是基于原始样本的程序,使用
和上下文作为参考
import requests
from bs4 import BeautifulSoup
def Download_Image_from_Web(url):
# name = random.randrange(1, 1000)
# fullName = str(name) + ".jpg"
source_code = requests.get(url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text, "html.parser")
raw_text = 'links.txt'
with open(raw_text, 'w') as fw:
for link in soup.findAll('img'):
image_links = link.get('src')
if '.jpg' in image_links:
for i in image_links.split("\\n"):
fw.write(i+'\n')
with open(raw_text, 'r') as fr:
text = fr.read()
# print(text)
print("Total {} images found:\n{}".format(len(text.splitlines()), text))
Download_Image_from_Web("https://pixabay.com/")
EDIT1:删除前一段中第一段中带有示例的描述
EDIT2:在输出中添加图像计数。解决方案已启动。但是,如果我们将最后一个read()移动到with context中,它将不会得到完整的内容,在这种特殊情况下,没有内容,因为在close()隐式处理之前没有强制刷新。将open(raw_text,'w')作为fw:for shoop中的链接。findAll('img'):image_links=link.get('src')if.jpg:for i in image_links.split(\\n”):fw.write(i+'\n'),将open(raw_text,'r')作为fr:text=fr.read()打印(文本)是的,我同意因此从我的答案中删除了和部分,现在如果你使用这个答案,你可以阅读书面文件的完整上下文。非常感谢你,它可以工作,但是每次在运行窗口中它都会不断添加一个链接,如:1121234…..我添加了这个代码:num_lines=sum(1表示打开的行('links.txt'))打印(num_lines)
效果很好,它将上次打印()中的计数添加到str.splitlines()
中以供参考。如果一次读取整个文件,则无需使用上下文,您可以直接使用file.read()
您可以使用with context逐行读取大文件。除非像这样使用with context一次性读取文件数据有点过分。context提供enter和exit,而不是iterable。file对象本身是一个迭代器。对于这个带有特定解释器的一次性脚本,并且将来不更新它,它可能是保留w/o close()很好。但是,按照一般惯例,显式关闭资源很重要。建议保留该规则以防止出现fd限制等问题。