Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.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 For循环返回空列表_Python_List_Web Scraping_Beautifulsoup - Fatal编程技术网

Python For循环返回空列表

Python For循环返回空列表,python,list,web-scraping,beautifulsoup,Python,List,Web Scraping,Beautifulsoup,我正在尝试使用功能pull_Brand从sptct_links中的链接中提取所有品牌: from selenium import webdriver from selenium.webdriver import Firefox from selenium.webdriver.firefox.options import Options from bs4 import BeautifulSoup import pandas as pd opts = Options() opts.headless

我正在尝试使用功能
pull_Brand
sptct_links
中的链接中提取所有品牌:

from selenium import webdriver
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options
from bs4 import BeautifulSoup
import pandas as pd

opts = Options()
opts.headless=True
assert opts.headless  # Operating in headless mode
browser = Firefox(options=opts)

sptct_links =['https://www.macys.com/shop/mens-clothing/mens-blazers-sports-coats/Productsperpage/120?id=16499',
        'https://www.macys.com/shop/mens-clothing/mens-blazers-sports-coats/Pageindex,Productsperpage/2,120?id=16499',
        'https://www.macys.com/shop/mens-clothing/mens-blazers-sports-coats/Pageindex,Productsperpage/3,120?id=16499',
        'https://www.macys.com/shop/mens-clothing/mens-blazers-sports-coats/Pageindex,Productsperpage/4,120?id=16499',
        'https://www.macys.com/shop/mens-clothing/mens-blazers-sports-coats/Pageindex,Productsperpage/5,120?id=16499']

def pull_Brand(url):
    browser.get(url)
    html = browser.page_source
    soup = BeautifulSoup(html, 'html.parser')
    brand = []
    for tag in soup.find_all(class_='productBrand'):
        brand.append(tag.text.strip())
    print(brand)

for i in range(len(sptct_links)):
        pull_Brand(sptct_links[i])

browser.quit()
虽然它成功地为
sptct\u链接[0]
在列表
brand
中添加了信息,但随后的列表返回为空。有没有关于为什么会发生这种情况以及如何修复的想法


非常感谢你

如果删除
opts=headless=True
选项,后续链接将出现以下错误:

拒绝访问 您没有访问此服务器上请求的URL的权限

参考:18.3d702617.1593528484.1aacf782

如果您要更改列表中链接的顺序,则无论第一个链接是什么,都将起作用,但随后的链接将失败。所以,链接本身并没有什么问题。我的猜测是,该网站检测到浏览器正在由自动化(Selenium)运行,并且只允许您进行一页的web访问


我在页面抓取之间暂停了5秒钟,但除了第一页之外,所有抓取都出现了错误,因此这不是抓取速率的问题。

如果删除
opts=headless=True
选项,后续链接将出现以下错误:

拒绝访问 您没有访问此服务器上请求的URL的权限

参考:18.3d702617.1593528484.1aacf782

如果您要更改列表中链接的顺序,则无论第一个链接是什么,都将起作用,但随后的链接将失败。所以,链接本身并没有什么问题。我的猜测是,该网站检测到浏览器正在由自动化(Selenium)运行,并且只允许您进行一页的web访问


我在页面抓取之间暂停了5秒钟,但除了第一个页面之外,所有抓取都出现了错误,因此这不是抓取速率的问题。

要获得正确的页面,请在请求中设置
用户代理
Http头

例如:

sptct_links =['https://www.macys.com/shop/mens-clothing/mens-blazers-sports-coats/Productsperpage/120?id=16499',
        'https://www.macys.com/shop/mens-clothing/mens-blazers-sports-coats/Pageindex,Productsperpage/2,120?id=16499',
        'https://www.macys.com/shop/mens-clothing/mens-blazers-sports-coats/Pageindex,Productsperpage/3,120?id=16499',
        'https://www.macys.com/shop/mens-clothing/mens-blazers-sports-coats/Pageindex,Productsperpage/4,120?id=16499',
        'https://www.macys.com/shop/mens-clothing/mens-blazers-sports-coats/Pageindex,Productsperpage/5,120?id=16499']

headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:77.0) Gecko/20100101 Firefox/77.0'}

for link in sptct_links:
    soup = BeautifulSoup(requests.get(link, headers=headers).content, 'html.parser')  # <-- set headers=

    for brand in soup.select('.productBrand'):
        print(brand.get_text(strip=True))

要获得正确的页面,请在请求中设置
User-Agent
Http头

例如:

sptct_links =['https://www.macys.com/shop/mens-clothing/mens-blazers-sports-coats/Productsperpage/120?id=16499',
        'https://www.macys.com/shop/mens-clothing/mens-blazers-sports-coats/Pageindex,Productsperpage/2,120?id=16499',
        'https://www.macys.com/shop/mens-clothing/mens-blazers-sports-coats/Pageindex,Productsperpage/3,120?id=16499',
        'https://www.macys.com/shop/mens-clothing/mens-blazers-sports-coats/Pageindex,Productsperpage/4,120?id=16499',
        'https://www.macys.com/shop/mens-clothing/mens-blazers-sports-coats/Pageindex,Productsperpage/5,120?id=16499']

headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:77.0) Gecko/20100101 Firefox/77.0'}

for link in sptct_links:
    soup = BeautifulSoup(requests.get(link, headers=headers).content, 'html.parser')  # <-- set headers=

    for brand in soup.select('.productBrand'):
        print(brand.get_text(strip=True))

刚刚测试过,您没有附加到brand,因为它找不到“productBrand”。w3.org/1999/xhtml“>访问被拒绝访问被拒绝您没有访问此服务器上请求的URL的权限 参考:18.95952f17.1593528432.1f2131d8

在使用chrome驱动程序时得到了这个。链接2-5。链接1工作正常。您可能遇到了速率限制,阻止您快速下载多个页面。他们不希望有人编写这样的web scraper。只需测试它,就不会附加到brand,因为它找不到“productBrand”。w3.org/1999/xhtml“>访问被拒绝访问被拒绝您没有访问此服务器上请求的URL的权限 参考:18.95952f17.1593528432.1f2131d8

在使用chrome驱动程序时得到了这个。链接2-5。链接1工作正常。您可能遇到了速率限制,阻止您快速下载多个页面。他们不想让人们写这样的网页刮刀。明白了,非常感谢!不一定会考虑到这一点,但有道理。明白了,非常感谢!不一定会考虑到这一点,但有道理。难以置信,这真的很有帮助!非常感谢。难以置信,这真的很有帮助!非常感谢。