简体   繁体   English

我将如何创建一个可以从应用程序内部更改 Python 代码的应用程序?

[英]How would I go about creating an app that can change python code from within the app?

This might be hard to explain so stay with me, I've created a web scraper that scrapes a specific site for information, the code looks like this -这可能很难解释,所以请听我说,我已经创建了一个网络抓取工具,可以抓取特定站点的信息,代码如下所示 -

import requests
from bs4 import BeautifulSoup
import pandas as pd 

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36'}

questionlist = []

def getQuestions(tag, page):
    url = f'https://www.merinfo.se/search?d=c&ap=1&emp=0%3A20&rev=0%3A100&who={tag}&bf=1&page={page}'
    r = requests.get(url, headers=headers)
    soup = BeautifulSoup(r.text, 'html.parser')
    questions = soup.find_all('div', {'class': 'box-white p-0 mb-4'})
    for item in questions:
        question = {
        'tag': tag,
        'title': item.find('a', {'class': 'link-primary'}).text,
        'link': item.find('a', {'class': 'link-primary'})['href'],
        'nummer': item.find('a', {'class': 'link-body'})['href'],
        'address': item.find('address', {'class': 'mt-2 mb-0'}).text,
        'RegÅr': item.find('div', {'class': 'col text-center'}).text,
        }
        questionlist.append(question)
    return

for x in range(1,6):
    getQuestions('bygg', x)
    #getQuestions('advokat', x)

df = pd.DataFrame(questionlist)
df.to_excel('merinfo skrapare för bygg.xlsx')
print('LBC Marketing TM')

what I would like to do is create a simple application that can change this part我想做的是创建一个可以改变这部分的简单应用程序

for x in range(1,6):
    getQuestions('bygg', x)
    #getQuestions('advokat', x)

mainly the ('bygg') and range (1,6) part.主要是 ('bygg') 和 range (1,6) 部分。 It doesn't have to be pretty but just something that works, and something that I can make pretty in the future.它不必很漂亮,而只是一些有用的东西,以及我将来可以做得漂亮的东西。 I'm not asking for a full on tutorial (although that would be nice) but just some pointers in the right direction so I know what to search for since I am really new to all this.我不是要一个完整的教程(虽然那会很好),但只是一些正确方向的指示,所以我知道要搜索什么,因为我对这一切都很陌生。

Cheers!干杯!

Just use a function and pass the parts you want to change as parameters.只需使用一个函数并将要更改的部分作为参数传递即可。 There is no need for "self modifying" or even generated code just for this:不需要为此“自我修改”甚至生成代码:


def get_results(field, start, stop):
    results = []
    for x in range(start, stop):
        results.append(getQuestions(field, x))
    return results

get_results('bygg', 1, 6)
get_results('advokat', 1, 10)

the values that get passed to get_results can come from a CSV file, or anyother place- just code that.传递给get_results的值可以来自 CSV 文件或任何其他地方 - 只需对其进行编码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我将如何在Python中创建另一个int()函数,以便我能理解它? - How would I go about creating another int() function in Python so I can understand it? 我将如何 go 创建一个从用户端重新启动代码的输入输入? - How would I go about creating an input input that restarts the code from the users end? Python-我将如何去做? - Python - How would I go about doing this? 我将如何 go 关于实现多个输入 - 在 if 语句中 - 在 Python 中使用列表理解 - How would I go about achieving multiple inputs - within if statements - with list comprehension in Python Python 2.7:如何创建一个字典,将前n个数字映射到它们各自的正方形或立方体? - Python 2.7: How would I go about creating a dictionary that maps the first n numbers to their respective squares or cubes? 在python中,我将如何为文本冒险创建保存系统? - In python, how would I go about creating a save system for a text adventure? 我将如何使用Python代码扩展我的PHP代码库? - How would I go about expanding my PHP code library with Python code? 我无法更改从文本文件生成的列表,该怎么办? Python 3.xx - I can't alter my list generated from a textfile, how would I go about doing this? Python 3.x.x 如何在pygame中创建流畅的相机运动? - How would I go about creating smooth camera movement in pygame? 我将如何在python中为Java代码编写非常简单的解析器? - How would I go about writing a very simplistic parser for java code in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM