Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我的代码应该同时查找关键字和颜色,但什么也没发生。知道为什么吗?_Python - Fatal编程技术网

Python 我的代码应该同时查找关键字和颜色,但什么也没发生。知道为什么吗?

Python 我的代码应该同时查找关键字和颜色,但什么也没发生。知道为什么吗?,python,Python,这是我目前掌握的代码: from selenium import webdriver from urllib.request import Request, urlopen from bs4 import BeautifulSoup driver = webdriver.Chrome(executable_path=r'mypath') req = Request('https://www.supremenewyork.com/shop/all/jackets', headers={'User-

这是我目前掌握的代码:

from selenium import webdriver
from urllib.request import Request, urlopen
from bs4 import BeautifulSoup
driver = webdriver.Chrome(executable_path=r'mypath')
req = Request('https://www.supremenewyork.com/shop/all/jackets', headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
soup = BeautifulSoup(webpage, 'lxml')
driver.get('https://www.supremenewyork.com/shop/all/jackets')
keywords = ('King Hooded Varsity Jacket')
color = ('Purple')
soup = BeautifulSoup(webpage, 'lxml')


for url in soup.find_all('a', href = True):
        text = url.get_text()
        print(text)
        if keywords in text:
            links = (url['href'])
            print('its there')
            print(links)

            if color in url.get_text():
                linkstwo = (url['href'])
                print('color is there')
                if linkstwo != links:
                    continue
                else:
                    print('both keywords and color have been found')
                    driver.get('https://www.supremenewyork.com/shop/all/jackets' + links)

这段代码在查找关键字和启动浏览器方面都不成功。有什么帮助吗?

这是代码。 这将访问每页(夹克)并打印
关键字
颜色

from selenium import webdriver
driver = webdriver.Chrome(executable_path=r'C:/.../cd79/chromedriver.exe')
driver.get('https://www.supremenewyork.com/shop/all/jackets')
driver.maximize_window()
page = driver.find_element_by_id('container')
a_ele = page.find_elements_by_tag_name('a')
links = [link.get_attribute('href') for link in a_ele]
for link in links:
    driver.get(link)
    keyword = driver.find_element_by_xpath('//*[@id="details"]/h1')
    print(keyword.text.strip())
    color = driver.find_element_by_class_name('style')
    print(color.text)
输出

Supreme®/The North Face®
S Logo Summit Series Himalayan Parka
Red
Supreme®/The North Face®
S Logo Summit Series Himalayan Parka
Black

奇怪的驱动程序仍然没有打开我想要的页面,也没有打印有关键字和颜色的页面。浏览器会打开,但不会打开项目。这些是我的输出:查看购物车查看/编辑购物车结账现在所有新夹克衬衫上衣/毛衣运动衫裤子短裤帽子包配件skate售罄Supreme®/North Face®S徽标Summit系列喜马拉雅山派克红色售罄Supreme®/North Face®S徽标Summit系列喜马拉雅山派克黑色售罄Supreme®/North Face®S徽标Summit系列喜马拉雅皮大衣莱姆已售罄Supreme®/North Face®S徽标山地夹克黑色已售罄Supreme®/North Face®S徽标山地夹克莱姆(还有更多项目我想找到该项目的href,然后使用href打开指向该项目的链接。我有点困惑。它只检查了3件夹克衫,而不是全部。@manatee不,它检查了所有夹克衫。您可能太快结束了程序。将输出添加到答案中以供参考。您要查找的信息是不在此页上。单击链接(夹克图片)后,它位于下一页上.
color
p
标签中,而不是
href
。关键字在下一页的
h1
标签中,也不是在
href
标签中。上面的代码无法为您提供
关键字
颜色
。您需要尝试单击一个夹克,从中刮取信息,然后将其放入l中哦,这还不可能吗?这是夹克页面上的一个元素:运气好吗?顺便说一句,谢谢你的帮助