【发布时间】:2018-10-26 20:16:26
【问题描述】:
我正在尝试下载他们在以下网站上拥有的所有钻石:https://www.bluenile.com/diamond-search?tag=none&track=NavDiaVAll
计划是获取信息并尝试找出我最喜欢购买的那一款(我会做一些回归来找出哪些有很大的价值并选择我最喜欢的)
为此,我编写了我的第一个刮板。问题是它似乎只拿了第 60 颗钻石,而不是我在网站上看到的所有钻石。理想情况下,我希望它能够带走所有 100k+ 的钻石,它们有不同的类型(圆形、垫形等)。 我如何让它给我所有的数据?
(我认为是因为一些新行只有在我向下滚动后才会加载,但我认为第一次加载超过 60,如果我向下滚动到底部,它只会显示 1000)
这是我的代码:
import pandas as pd
import requests
from bs4 import BeautifulSoup
url = 'https://www.bluenile.com/diamond-search?tag=none&track=NavDiaVAll'
url_response = requests.get(url)
soup = BeautifulSoup(url_response.content, "html.parser")
""" Now we have the page as soup
Lets start to get the header"""
headerinctags = soup.find_all('div', class_='grid-header normal-header')
header = headerinctags[0].get_text(';')
diamondsmessy = soup.find_all('a', class_='grid-row row ')
diamondscleaned = diamondsmessy[1].get_text(";")
"""Create diamonds dataframe with the header; take out the 1st value"""
header = header.split(";")
del header[0]
diamonds = pd.DataFrame(columns=header)
""" place rows into dataframe after being split; use a & b as dummy variables; take out 5th value"""
for i in range(len(diamondsmessy)):
a = diamondsmessy[i].get_text(";")
b = a.split(";")
del b[4]
a = pd.DataFrame(b, index=header)
b = a.transpose()
diamonds = pd.concat([diamonds, b], ignore_index=True)
print(diamonds)
【问题讨论】:
标签: python python-3.x web-scraping beautifulsoup python-requests