【问题标题】:How would I input data to a website text box, and then write that data to a text file?如何将数据输入网站文本框,然后将该数据写入文本文件?
【发布时间】:2020-03-16 17:59:18
【问题描述】:

我正在编写一个程序,根据用户输入的单词打印出 ASCII 艺术。这个词被翻译成一个图片链接,此时,我正在尝试将此链接发送到https://www.ascii-art-generator.org/。这是行不通的。从这里开始,我尝试读取生成的 ASCII 艺术作品,然后将其放入可以打印的文本文件中。这也不起作用。我无法将链接发送到网站然后检索该数据。我将如何使用https://www.ascii-art-generator.org/ 实现这一目标?

import urllib
import requests
from googlesearch import search
import os
import sys
import time

os.system(["clear", "cls"][os.name == "nt"])

image = input("What do you want me to draw?\n\x1b[5m-\x1b[25m ")

query = image + " images"

print("\n\x1b[5mDrawing...\x1b[25m ", end="")

for image_link in search(query, tld="co.in", num=10, stop=1, pause=2): 
    print(image_link)

ascii_generator = "https://www.ascii-art-generator.org/"

website_data = {"name" : image_link, 
"type" : "text",
"id" : "fileupfield-url",
"class" : "width-input ff-inputfield"}

r = requests.post(ascii_generator, data = website_data)

with open("art.txt", "wb") as code:
    code.write(r.content)

【问题讨论】:

    标签: python web-scraping request ascii urllib


    【解决方案1】:

    您正在发送正确的请求,但参数错误,并且该发布请求会将您带到另一个页面,而不是直接的艺术。即使在您到达此页面后,仍需要一段时间才能加载艺术作品。该网站正在发送一个请求以检查艺术是否准备就绪(可以在开发工具中看到)。这个过程可以很容易地在 python 中复制,因为发送的图像的名称存储在网站上。你需要 beautifulsoup 才能工作 (pip install bs4)

    代码

    import requests, re, time
    from bs4 import BeautifulSoup
    
    ascii_generator = "https://www.ascii-art-generator.org/"
    image_link = 'https://i.guim.co.uk/img/media/2589fa711843a42405ae233b71f85ead362f6659/0_103_2160_1296/master/2160.jpg?width=300&quality=85&auto=format&fit=max&s=a52d11a9ace574f1927043f8f66a6032'
    
    website_data = {
        'art_type': 'mono',
        'userfile': '(binary)',
        'userfile_url': image_link,
        'banner_text': '',
        'outFormat_caca': 'html',
        'figlet_font': 0,
        'width': 300,
        'banner_width': 100,
        'user_screen_width': 980,
    }
    
    r = requests.post(ascii_generator, data=website_data)
    soup = BeautifulSoup(r.text, 'html.parser')
    script = [script.text for script in soup.find_all('script') if 'var url' in script.text][0]
    
    name = re.search(r"name=(\w*)", script).group(1)
    now = int(time.time())
    
    check_url = 'https://www.ascii-art-generator.org/FW/result.php'
    params = {
        'name': name,
        'tscachebusttamp': now
    }
    check = '__wait__123'
    while check == '__wait__123':
        check = requests.get(check_url, params=params).text
        time.sleep(3)
        print('checked')
    
    check_soup = BeautifulSoup(check, 'html.parser')
    art_link = 'https://www.ascii-art-generator.org' + check_soup.find('a', text='banner.txt')['href']
    art = requests.get(art_link)
    with open('art.txt', 'wb') as code:
        code.write(art.content)
        print('\nArt saved!')
    

    【讨论】:

    • 这几乎对我有用,但我收到此错误:回溯(最近一次调用最后一次):文件“artist.py”,第 84 行,在 art_link = 'ascii-art-generator.org' + check_soup.find('a', text='banner.txt')['href'] TypeError: 'NoneType' object is not subscriptable 我不知道为什么。你能解释一下它为什么这样做吗?对不起,我不明白。谢谢!
    • 如果您只尝试一次,服务器可能已经超载。你有没有从脚本中改变任何东西?如果不是,请尝试删除 , text='banner.txt'
    • 我修改了脚本 - 并将其与原始脚本相结合。我马上去测试!
    • 你能在while循环之后打印变量检查并将html发送到pastebin吗?它对我有用,所以除非我看到失败的 html,否则我不知道发生了什么。
    • 好的 - 我将包括一个。当它只是您的代码时,它可以工作 - 但是,它有两个方面会失败。当我将颜色从“单色”更改为“彩色”时,它失败了,发生的事情是使用不同的图像 URL,它不起作用。这是 pastebin:pastebin.com/wjaHwfyg 谢谢大家的帮助,我真的很感激!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-28
    • 1970-01-01
    • 1970-01-01
    • 2011-12-06
    • 2014-06-02
    • 1970-01-01
    • 2013-05-08
    相关资源
    最近更新 更多