简体   繁体   English

if语句与bs4输出中的String匹配

[英]If statement to match String in bs4 output

Need some assistance. 需要帮助。 I have a function where I use the find_all function on my bs object to print some specific html. 我有一个函数,在我的bs对象上使用find_all函数来打印一些特定的html。 my goal here is to eventually match the productName and color together so I can go to the correct link but I am having some trouble picking out the correct word. 我的目标是最终将productName和color一起匹配,这样我可以转到正确的链接,但是在选择正确的单词时遇到了一些麻烦。 Here is my code and I get no output when I run this. 这是我的代码,运行此程序时没有输出。

def getPremeProduct() -> str:
session = requests.Session()
base = "http://www.supremenewyork.com"
r = session.get('http://www.supremenewyork.com/shop/all/accessories')
soup = BeautifulSoup(r.content, 'lxml')
keyword = input('Input your keyword: ')
color = str(input('input your color: '))
productColor = soup.findAll('p', text=re.compile(r'\bChecker\b'))
productName = soup.findAll('a', text=re.compile(r'\bTees\b'))

if "Checker" in productName:
    print(productName)

### Here are the outputs for productName
#<a class="name-link" href="/shop/accessories/s4o3fjcg0/eq35gubw7">Supreme®/Hanes® Checker Tagless Tees (2 Pack)</a>
#<a class="name-link" href="/shop/accessories/hz7wj2ui4/jx7qm1hsc">Supreme®/Hanes® Tagless Tees (3 Pack)</a>
#<a class="name-link" href="/shop/accessories/hz7wj2ui4/yks6zay73">Supreme®/Hanes® Tagless Tees (3 Pack)</a>

Anyone have any ideas why my if statement isn't printing out just the one result that has "Checker" included? 任何人都知道为什么我的if语句不仅仅打印出包含“ Checker”的结果吗? Thanks 谢谢

There are some problems with your code: 您的代码存在一些问题:

  1. You should indent your code when defining a function. 定义函数时应缩进代码。 Ie code under def should be indented wrt. def下的代码应缩进wrt。 the def keyword. def关键字。
  2. why use input if you are not using it later? 如果以后不使用input为什么要使用input
  3. pep 8 specifies a naming convention for variable names. pep 8指定了变量名称的命名约定。 It might be a good idea to follow that. 遵循它可能是一个好主意。 To be more specific: 更加具体:

Function names should be lowercase, with words separated by underscores as necessary to improve readability. 函数名称应小写,必要时用下划线分隔单词,以提高可读性。

Variable names follow the same convention as function names. 变量名与函数名遵循相同的约定。

Going back to your problem, the most straight forward way of doing what you want is: 回到您的问题,做您想要做的最直接的方法是:

import requests
from bs4 import BeautifulSoup
import re

def get_product():
    session = requests.Session()
    r = session.get('http://www.supremenewyork.com/shop/all/accessories')
    soup = BeautifulSoup(r.content, 'lxml')
    product_names = soup.findAll('a', text=re.compile(r'\bTees\b'))
    product_names = [link.get_text() for link in product_names]
    return product_names

products = get_product()
for item in products:
    if 'Checker' in item:
        print('yas gurl!')

soup.findAll finds mutiple elements. soup.findAll查找多个元素。 You therefore need to iterate them: 因此,您需要对其进行迭代:

import requests
from bs4 import BeautifulSoup

def getPremeProduct() -> str:
    session = requests.Session()
    base = "http://www.supremenewyork.com"
    r = session.get('http://www.supremenewyork.com/shop/all/accessories')
    soup = BeautifulSoup(r.content, 'lxml')
    keyword = input('Input your keyword: ')
    color = str(input('input your color: '))
    productColor = soup.findAll('p', text=re.compile(r'\bChecker\b'))
    productNames = soup.findAll('a', text=re.compile(r'\bTees\b'))

for product_name in ProductNames:
    product_name = str(product_name)
    if "Checker" in product_name:
        print(product_name)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM