我如何将数据输入到网站文本框中,然后将数据写入文本文件?

时间:2019-11-20 22:09:51

标签: python web-scraping request ascii urllib

我正在编写一个程序,该程序根据用户输入的单词打印出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)

1 个答案:

答案 0 :(得分: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!')