运行我的代码后,我没有得到“更新”的数据后,点击加载更多按钮。我尝试了很多次,我不知道我的代码是什么错误。我应该有一个数据列表后,加载更多按钮运行在python中。然而,我只得到了数据之前,按钮被点击,我附加在列表中。
class RunChromeTests():
def test(self):
chrome_options = Options()
chrome_options.add_argument("--disable-notifications")
chrome_options.add_argument("-incognito")
chrome_options.add_argument("--disable-popup-blocking")
chrome_options.add_argument("--ignore-certificate-errors")
chrome_options.add_argument("--disable-javascript")
# Download the chrome driver from https://chromedriver.chromium.org/downloads
# and find the driver location in your computer
chrome_path = r"path"
driver = webdriver.Chrome(chrome_path, options=chrome_options)
driver.maximize_window()
driver.implicitly_wait(10)
final = []
### Enter your url to scrape but change the page number to {a}
driver.get("https://www.burpple.com/search/sg?q=Newly+Opened&type=places")
content = driver.page_source
soup = BeautifulSoup(content)
loadmore = driver.find_element_by_id("masonryViewMore-btn")
j = 0
final1=[]
try:
while loadmore.is_displayed():
loadmore.click()
time.sleep(2)
lrec = soup.find_all("span",{"searchVenue-header-name-name headingMedium"})
#loadmore.is_displayed()
newlist = lrec[j:]
print(lrec)
#print(newlist)
for rec in newlist:
name = rec.text
#print(name)
final1.append(name)
print(final1)
j = len(lrec)+1
#final1.append(name)
time.sleep(5)
#print(j)
#
except exceptions.StaleElementReferenceException:
pass
chromed = RunChromeTests()
chromed.test()
输出:【“Kotuwa”、“Smoochie Creamery”、“Evan 's Kitch”、“Plus Coffee Joint”、“800°木烤比萨(KINEX)”、“Sarah' s Loft”、“nicher(Springleaf)”、“First Story Cafe”、“Tucela Gelato”、“Ri Ri Cha”、“Unatoto”、“皇家棕榈(肉类和餐饮)”】
正确输出:[“Kotuwa”、“Smoochie Creamery”、“Evan 's Kitch”、“Plus咖啡连锁店”、“800°木烤比萨饼(KINEX)“,“莎拉的阁楼”,“尼克(Springleaf)“、”第一故事咖啡馆“、”图塞拉凝胶“、”Ri Ri Cha“、”Unatoto“、”皇家棕榈(肉类与餐饮)“、”华丽的面包房“、”等同于咖啡(果园中心)','TAG Espresso(莱佛士城)、“Arc-En-Ciel糕点店”、"享受餐厅和酒吧“(史蒂文斯)、”土井康典SAGE(果园广场)“、”Hellu咖啡“、”杵和迫击炮协会“,...]
... -〉表示&更多
1条答案
按热度按时间3lxsmp7m1#
你需要在每次点击
Lode More
按钮时获取页面源代码。并不断检查名称是否存在于列表中,如果没有,然后添加。使用无限循环并检查
load more
按钮是否存在,如果存在,则单击其他按钮中断循环。代码:
你需要导入下面的库.
输出: