繁体   English   中英

Python 美汤 find_all()

[英]Python Beautiful Soup find_all()

我正在尝试在下面的 html 上使用 find_all();

http://www.simon.com/mall

根据其他线程的建议,我通过以下站点运行链接并发现错误,但我不确定显示的错误如何影响我在 Beautiful Soup 中尝试做的事情。

https://validator.w3.org/

这是我的代码;

from requests import get

url = 'http://www.simon.com/mall'
response = get(url)

from bs4 import BeautifulSoup

html = BeautifulSoup(response.text, 'html5lib')
mall_list = html.find_all('div', class_ = 'col-xl-4 col-md-6 ')

print(type(mall_list))
print(len(mall_list))

结果是;

"C:\Program Files\Anaconda3\python.exe" C:/Users/Chris/PycharmProjects/IT485/src/GetMalls.py
<class 'bs4.element.ResultSet'>
0

Process finished with exit code 0

我知道 HTML 中有数百个这样的 div。 为什么我没有得到任何匹配?

我有时也会使用 BeautifulSoup。 问题在于你获取属性的方式。 完整的工作代码如下所示:

import requests
from bs4 import BeautifulSoup

url = 'http://www.simon.com/mall'
response = requests.get(url)
html = BeautifulSoup(response.text)
mall_list = html.find_all('div', attrs={'class': 'col-lg-4 col-md-6'})[1].find_all('option')
malls = []

for mall in mall_list:
    if mall.get('value') == '':
        continue
    malls.append(mall.text)

print(malls)
print(type(malls))
print(len(malls))

您的代码看起来不错,但是,当我访问 simon.com/mall 链接并检查 Chrome Dev Tools 时,似乎没有“col-xl-4 col-md-6”类的任何实例。

尝试使用“col-xl-2”测试您的代码,您应该会看到一些结果。

假设您正在尝试从该页面(在您的脚本中提到)解析不同产品的标题和位置。 问题是该页面的内容是动态生成的,因此您无法通过请求捕获它; 相反,您需要使用任何浏览器模拟器,如 selenium,这是我在下面的代码中所做的。 试试这个:

from selenium import webdriver
from bs4 import BeautifulSoup
import time

driver = webdriver.Chrome()
driver.get('http://www.simon.com/mall')
time.sleep(3)

soup = BeautifulSoup(driver.page_source, 'lxml')
driver.quit()

for item in soup.find_all(class_="mall-list-item-text"):
    name = item.find_all(class_='mall-list-item-name')[0].text
    location = item.find_all(class_='mall-list-item-location')[0].text
    print(name,location)

结果:

ABQ Uptown Albuquerque, NM
Albertville Premium Outlets® Albertville, MN
Allen Premium Outlets® Allen, TX
Anchorage 5th Avenue Mall Anchorage, AK
Apple Blossom Mall Winchester, VA

暂无
暂无

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

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