我如何在HTML中放置一个值并使用该值作为变量执行python代码

时间:2022-11-22 21:42:15

I found this Python code:

我找到了这个Python代码:

#! C:\python27

import urllib2
from bs4 import BeautifulSoup
url='http://www.google.com/'

conn = urllib2.urlopen(url)
html = conn.read()  

soup = BeautifulSoup(html)
links = soup.find_all('a')

for tag in links:
    link = tag.get('href',None)
    if link != None:
        print link

And I need to make an HTML page that asks for the url value so when I type it, it changes the value on the python file and crawl the page I typed. I have a simple HTML code that asks the user for the URL he wants to crawl and has a input text so he can type what he wants, but how can I make the value that the user types to be the value of the url variable.

我需要创建一个HTML页面,询问url值,这样当我输入它时,它会更改python文件的值并抓取我输入的页面。我有一个简单的HTML代码,询问用户他想要抓取的URL并有一个输入文本,以便他可以键入他想要的内容,但是如何将用户键入的值设置为url变量的值。

<form name="input" action="#" method="post">
Type in the page you want to crawl:<input type="text" name="url" value="http://www.google.com/"><br/>
<input type="submit" value="Submit">
</form>

How can I do that? New to Python, I have only worked with JavaScript before.

我怎样才能做到这一点? Python新手,我之前只使用过JavaScript。

1 个解决方案

#1


1  

Since the user has direct access to the Python script, there is no need to go through an HTML form.

由于用户可以直接访问Python脚本,因此无需通过HTML表单。

You can ask for the user to input the url directly to the Python program.

您可以要求用户将URL直接输入到Python程序中。

Just write:

url = raw_input("Please enter a URL to crawl: ")

#1


1  

Since the user has direct access to the Python script, there is no need to go through an HTML form.

由于用户可以直接访问Python脚本,因此无需通过HTML表单。

You can ask for the user to input the url directly to the Python program.

您可以要求用户将URL直接输入到Python程序中。

Just write:

url = raw_input("Please enter a URL to crawl: ")