我需要两个函数 my_parse_eng 和 my_parse_rus 来解析站点,这没有问题。然后我不知道如何将接收到的数据发送到 my_open(): 函数,该函数将所有内容写入 text.csv 文件
在main函数中,我两次访问同一个链接,如何正常访问,不重复?
import requests
from lxml import etree
import lxml.html
import csv
def my_open():
with open("text.csv", "w", newline='') as csv_file:
write = csv.writer(csv_file)
for i in range(len(my_parse_eng())):
write.writerow(my_parse_eng([i]))
write.writerow(my_parse_rus([i]))
def my_parse_eng(url):
api = requests.get(url)
tree = lxml.html.document_fromstring(api.text)
text_original = tree.xpath('//*[@id="click_area"]//*[@class="original"]/text()')
return(text_original)
def my_parse_rus(url):
api = requests.get(url)
tree = lxml.html.document_fromstring(api.text)
text_translate = tree.xpath('//*[@id="click_area"]//*[@class="translate"]/text()')
return(text_translate)
def main():
my_parse_eng("https://www.amalgama-lab.com/songs/t/tones_and_i/dance_monkey.html")
my_parse_rus("https://www.amalgama-lab.com/songs/t/tones_and_i/dance_monkey.html")
my_open()
if __name__ == "__main__":
main()
您
main从函数中获取数据,将其保存到变量中,然后通过参数将其传递给my_open: