有没有办法在for循环中执行字符串?

2024-05-14 18:53:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经创建了一个数组,其中包含所有可能的html标记,当运行该标记时,它将导航到目标字符串,或者什么都没有

group = ['div','span','a','link','dl','dt','dd','b','p','meta','']
comb = []

for g1 in group:
    if g1 != '':
        for g2 in group:
            if g2 != '':
                for g3 in group:
                    if g3 != '':
                        res = "tag."+g1+"."+g2+"."+g3+".string"
                        comb.append(res)
                    else:
                        res = "tag."+g1+"."+g2+".string"
                        comb.append(res)
            else:
                res = "tag."+g1+".string"
                comb.append(res)    

我想运行数组中的每个条目,查看它从给定网站返回的内容

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

    page = requests.get(url, headers=headers)
    soup = BeautifulSoup(page.content, "lxml")
    tag = soup.find(class_=re.compile("price"))

    for c in comb:
        exec(c, globals())

有没有一种方法可以像exec()那样以代码的形式运行列表中的字符串? 我在Python3上使用BeautifulSoup、Requests、Googlesearch和Re


Tags: in标记forstringiftaggroupres
1条回答
网友
1楼 · 发布于 2024-05-14 18:53:35

对于动态属性访问,您不需要exec()eval()使用getattr(),或者在BeautifulSoup的情况下,使用方法find()获取与给定条件匹配的第一个子项:

from itertools import chain, product

group = ['div','span','a','link','dl','dt','dd','b','p','meta']
# Produce a list of tuples of element names
comb = list(chain(*[product(*[group] * n) for n in range(1, 4)]))

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

    page = requests.get(url, headers=headers)
    soup = BeautifulSoup(page.content, "lxml")
    tag = soup.find(class_=re.compile("price"))

    for c in comb:
        t = tag
        for a in c:
            t = t.find(a)
            if not t:
                break

        if not t:
            continue

        # Do something with t.string
        t.string

你也可以使用select()来限制同样的效果,我认为:

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

    page = requests.get(url, headers=headers)
    soup = BeautifulSoup(page.content, "lxml")
    tag = soup.find(class_=re.compile("price"))

    for c in comb:
        selector = ' '.join(c)
        r = tag.select(selector, limit=1)
        if r:
            r = r[0]

        else:
            continue

        r.string

至于刮掉谷歌搜索结果是不是一个好主意,我不偏袒任何一方

相关问题 更多 >

    热门问题